对于一些python练习,我决定研究计算器教程.这是非常基本的,所以我决定在用户输入垃圾时给它进行异常处理.虽然正确使用该程序仍然有效,但在废话中打孔仍然会导致它崩溃,输入以下是我的代码:
loop = 1
choice = 0
while loop == 1:
#print out the 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: ")
try:
if choice == 1:
add1 = input("add this: ")
add2= input("to this: ")
print add1, "+", add2, "=", add1+ add2
elif choice == 2:
sub1 = …Run Code Online (Sandbox Code Playgroud)