你好我是python
的新手。我正在进行基本的python教程并在python
中制作一个简单的计算器,但它不起作用。它是:
#calculator program
#this variable tells the loop whether it should loop or not.
# 1 means loop. anything else means don't loop.
loop = 1
#this variable holds the user's choice in the menu:
choice = 0
while loop == 1:
#print what options you have
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 (" ")
choice = input("Choose your option: ")
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print (add1, "+", add2, "=", add1 + add2)
elif choice == 2:
sub2 = input("Subtract this: ")
sub1 = input("from this: ")
print (sub1, "-", sub2, "=", sub1 - sub2)
elif choice == 3:
mul1 = input("Multiply this: ")
mul2 = input("with this: ")
print (mul1, "*", mul2, "=", mul1 * mul2)
elif choice == 4:
div1 = input("Divide this: ")
div2 = input("by this: ")
print (div1, "/", div2, "=", div1 / div2)
elif choice == 5:
loop = 0
print ("Thankyou for using calculator.py!")
按F5
运行即将到来的
Welcome to calculator.py
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator.py
Choose your option:
选择1,它应该提示输入数字,但它会返回并显示
Welcome to calculator.py
your options are:
我认为当输入1或2或3或4或5并且返回时它无法截取我的选项choise
。它有什么问题
答案 0 :(得分:1)
choice = input("Choose your option: ")
当您输入选项1
时。它需要1
作为字符串,但是你的比较字符串是int。
只需将您的输入转换为int
choice = int(input("Choose your option: "))