我试图创建一个简单的计算器,但是我使用的IDE一直告诉我我要用字符串执行数学运算。我试着把所有的变量都赋给双精度的,但没用。在
"""Calculator program"""
loop = 1 # 1 means loop; anything else means don't loop.
choice = 0 # This variable holds the user's choice in the menu
add1 = 0.0
add2 = 0.0
sub1 = 0.0
sub2 = 0.0
mul1 = 0.0
mul2 = 0.0
div1 = 0.0
div2 = 0.0
result = 0.0
while loop == 1:
# Print the options user has
print ("Welcome to calculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) Quit calculator.py")
print (" ")
#Perform the desired operation
choice = int(input("Choose your option: ").strip())
if choice == 1:
add1 = input()
add2 = input()
result = add1 + add2
print(add1, add2)
print (add1, "+", add2, "=", result)
elif choice == 2:
sub2 = input()
sub1 = input()
result = sub1 - sub2
print (sub1, "-", sub2, "=", result)
elif choice == 3:
mul1 = input("Multiply this: ")
mul2 = input("with this: ")
result = mul1 * mul2
print (mul1, "*", mul2, "=", result)
elif choice == 4:
div1 = input("Divide this: ")
div2 = input("by this: ")
result = div1 / div2
print (div1, "/", div2, "=", result)
elif choice == 5:
loop = 0
print ("Thank-you for using calculator.py!")
input()
返回字符串,而不是数字。你必须把它转换成:或者
^{pr2}$取决于你希望你的计算器支持什么,否则你实际上是在用字符串执行数学运算。在
试着粘上
在它上面制造
^{pr2}$希望这有帮助
相关问题 更多 >
编程相关推荐