你好,用户8311329,
问题
当您使用字符串作为输入时,请使用raw_input()函数。而且您正在使用的所有程序都使用;,所以python提供了一个带有out ;(colon)来执行程序的功能。在
解决方案
试试这个代码while True:
print("please choose one of these available options: \n1. Add \n2. Subtract \n3. Multiply \n4. Divide \n5. Quit \nType in exactly as shown or the calculator won't work")
choice = str(raw_input("From above choice any one option: "))
if(choice == "quit"):
break
elif(choice == "divide"):
num1 = float(input("Please enter a number:"))
num2 = float(input("Please enter another number:"))
output = num1/num2
print "The answer is:",output
elif(choice == "multiply"):
num1 = float(input("Please enter a number:"))
num2 = float(input("Please enter another number:"))
output = num1*num2
print "The answer is:",output
elif(choice == "subtract"):
num1 = float(input("Please enter a number:"))
num2 = float(input("Please enter another number:"))
output = num1-num2
print "The answer is:",output
elif(choice == "add"):
num1 = float(input("Please enter a number:"))
num2 = float(input("Please enter another number:"))
output = num1+num2
print "The answer is:",output
else:
print("Invalid Input. Try again")
例如,如果有其他用户运行此程序,请使用您的应用程序,以便此人不了解输入的确切内容,因此代码编写高效且完美,以便其他用户容易理解并给出建议,不要使用此代码插入字符串,而是使用数字输入进行选择,就像我给出的示例(我不只是给出简单的示例而且您还添加了更多的输入验证)
^{pr2}$
我希望我的回答有帮助。
如有任何疑问请评论。在