问题描述
正如我的代码可以告诉我的那样,我想做一个基本的计算器,首先要问你要做什么,然后问2输入2的数字由代码计算出来。我遇到了一个问题,代码没有返回任何内容,甚至都没有尝试使用它想要的功能。
##Simple Calculator program##
print("Welcome to my basic calculator program")
print("In this program you will be asked to input what function you want to do then")
print("select 2 numbers,where the program will then do the mathematic operation on those 2 numbers")
#Class containing the functions and basic caluclation
class calculator_class():
print("Please select a function by entering these :")
print("Addition")
print("Subtraction")
print("Multiplication")
print("Division")
#this is a function which asks the user to choose what operator to choose before choosing their number
def userchoice():
userchoices = str(input())
if userchoices in ["Addition","Subtraction","Multiplication","Division"]:
return(
if userchoices == "Addition":
print(addition())
elif userchoices == "Subtraction":
print(subtraction())
elif userchoices == "multiplication":
print(multiplication())
elif userchoices == "division":
print(division())
else:
print(invalid_choice())
print(userchoice())
#here the user chooses the 2 numbers
print("Please select 2 numbers to calculate")
usernumber1 = int(input("Please input your first number here : "))
usernumber2 = int(input("Please input your second number here : "))
#Functions of which contain addition,subtraction,multiplication and division
def addition():
print("A D D I T I O N")
print("Just calculating...")
print(usernumber1 + usernumber2)
def subtraction():
print("S U B T R A C T I O N")
print("Just calculating...")
print(usernumber1 - usernumber2)
def multipliction():
print("M U L T I P L I C A T I O N ")
print("Just calculating...")
print(usernumber1 * usernumber2)
def division():
print("D I V I S I O N ")
print("Just calculatin...")
print(usernumber1 / usernumber2)
def invalid_choice():
print("You did not pick a valid option,please try again")
解决方法
您在此代码中有很多错误的方法。
- 键入一个符号(例如*)比输入“乘法”更容易
- 最好为每个用户输入应用
#include <stdio.h> int main() { int input; long double result = 1; printf("Enter the integer number : "); scanf("%d",&input); printf("n:!\n\n"); for(int i = 1; i <= input; i++) { result *= i; if(i%2==1){ printf("%d:%Le\n",i,result); } //note that im using %Le for long double type }
- python中的输入始终为str,因此
.lower()
中的str()
是多余的 -
userchoices = str(input())
的{{1}}可能会导致错误(int()
),因此请将此类代码放在input()
块中