Python计算器除以零/取消否定.诠释.崩溃的程序

好吧,我正在为一个项目做这个,每当我试图将它除以零或平方根时,程序关闭的负数.我试图找到一些可以插入到代码中的内容,使其显示一条消息,然后再次提示输入值,但是我尝试插入的所有内容都会导致程序在启动时立即关闭.

这是计算器没有插入任何东西来修复崩溃.

import math

def convertString(str):
    try:
        returnValue = int(str)
    except ValueError:
        returnValue = float(str)
    return returnValue

def addition(a, B):
    return convertString(a) + convertString(B)

def subtraction(a, B):
    return convertString(a) - convertString(B)

def multiplication(a, B):
    return convertString(a) * convertString(B)

def division(a, B):
    return convertString(a) / convertString(B)

def sqrt(a):
    return math.sqrt(convertString(a))

keepProgramRunning = True

print "Welcome to [Removed]'s 2011 4-H Project! This is a simple calculator coded in  Python, which is a high-level programming language. Java, C, C++, and Perl are  other high-level programming languages that you may have heard of."

while keepProgramRunning:
    print ""
    print "Please choose what you would like to do:"
    print ""
    print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Square Root"
    print "6) Quit Program"

    choice = raw_input()    

    if choice == "1":
        numberA = raw_input("Enter your first addend: ")
        numberB = raw_input("Enter your second addend: ")
        print "The sum of those numbers is:"
        print addition(numberA, numberB)
    elif choice == "2":
        numberA = raw_input("Enter your first term: ")
        numberB = raw_input("Enter your second term: ")
        print "The difference of those numbers is:"
        print subtraction(numberA, numberB)
    elif choice == "3":
        numberA = raw_input("Enter your first factor: ")
        numberB = raw_input("Enter your second factor: ")
        print "The product of those numbers is:"
        print multiplication(numberA, numberB)
    elif choice == "4":
        numberA = raw_input("Enter your dividend: ")
        numberB = raw_input("Enter your divisor: ")
        print "The quotient of those numbers is:"
        print division(numberA, numberB)
    elif choice == "5":
        numberA = raw_input("Enter the number you wish to find the square root of: ")
        print "Your result is:"
        print sqrt(numberA)
    elif choice == "6":
        print "Goodbye! Thank you for your time spent both judging my project and those of everyone else! Have a nice day! (。◕‿◕。)"
        keepProgramRunning = False
    else:
        print "Please choose a valid option."
        print "\n"

我不确定要插入什么以及在哪里解决崩溃,但我认为问题在于我的位置.

我一直试图插入这样的东西:

except ValueError:
            print "You cannot divide by zero. Please choose another divisor."
            numberB = raw_input("Enter your divisor: ")

那会有用吗?我会在哪里插入它?如果它不起作用,会发生什么,它会去哪里?

我一直试图把它放在后面

numberB = raw_input("Enter your divisor: ")

那部分会读

elif choice == "4":
    numberA = raw_input("Enter your dividend: ")
    numberB = raw_input("Enter your divisor: ")
        except ValueError:
            print "You cannot divide by zero. Please choose another divisor."
            numberB = raw_input("Enter your divisor: ")
    print "The quotient of those numbers is:"
    print division(numberA, numberB)

但正如我所说的那样,程序会在我尝试时立即关闭.另外,我知道如果他们再次输入0,程序就会崩溃.有没有办法让它回到它所在的线上?

此外,为了关闭程序,由于程序在显示后立即关闭,或者命令正在同时执行,因此无法读取它应显示的消息.无论哪种方式,都无法读取消息.有没有办法让消息出现在一个单独的对话窗口中,当窗口关闭时会导致程序关闭?或者至少是一种让它在关闭之前延迟的方法?

如果我的术语有错,请纠正我.我还是有些新手.

并且,一如既往,对于程序的任何部分的(建设性的)反馈总是受到赞赏.

解决方法:

问题是你试图在抛出异常之前捕获它.相反,试试这个:

elif choice == "4":
    numberA = raw_input("Enter your dividend: ")
    numberB = raw_input("Enter your divisor: ")
    while float(numberB) == 0:
        print "You cannot divide by zero. Please choose another divisor."
        numberB = raw_input("Enter your divisor: ")
    print "The quotient of those numbers is:"
    print division(numberA, numberB)
上一篇:java – 赋予括号优先权


下一篇:c# – Microsoft标准计算器中使用的变量类型