小编use*_*649的帖子

计算器异常处理,为什么这个例子工作但我的不工作?

对于一些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)

python exception-handling calculator

1
推荐指数
1
解决办法
1512
查看次数

标签 统计

calculator ×1

exception-handling ×1

python ×1