3

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

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

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,程序就会崩溃。有没有办法让它回到它所在的行?

此外,对于关闭程序,它应该显示的消息无法读取,因为程序在显示后立即关闭,或者正在同时执行命令。无论哪种方式,都无法阅读该消息。有什么方法可以使消息出现在单独的对话框窗口中,从而导致程序在窗口关闭时关闭?或者至少有一种方法可以让它在关闭前延迟?

如果我有任何术语错误,请纠正我。我对此还是有些陌生。

而且,与往常一样,对程序任何部分的(建设性)反馈总是值得赞赏的。

4

5 回答 5

3

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

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)
于 2011-07-13T01:58:26.113 回答
2

这是您的答案,没有异常处理。本质上,您只是在无限循环开始时输入了这个有问题的输入,但是当您发现输入都很好时就会中断。

对于您的平方根:

elif choice == "5":
    while True:
        numberA = raw_input("Enter the number you wish to find the square root of: ")
        if float(numberA) >= 0:
            break
        print "Cannot take the square root of a negative number."
    print "Your result is:", sqrt(numberA)

和划分:

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

并使您的程序在关闭之前停止:

elif choice == "6":
    print "Goodbye! Thank you for your time spent both judging my project and those of everyone else! Have a nice day! (。◕‿◕。)"
    raw_input('')
    keepProgramRunning = False

干杯!

于 2011-07-13T02:06:51.620 回答
1

您应该使用异常,就像您已经在convertString.

try:
    result = division(numberA, numberB)
    print "The quotient of those numbers is:"
    print result
except ZeroDivisionError:
    print "You cannot divide by zero. Please choose another divisor."
于 2011-07-13T02:01:42.100 回答
0

您的声明格式try:...except:错误。试试这个:

elif choice == "4":
    numberA = raw_input("Enter your dividend: ")
    numberB = raw_input("Enter your divisor: ")
    print "The quotient of those numbers is:"
    try:
        print division(numberA, numberB)
    except ZeroDivisionError:
        print "You cannot divide by zero. Please choose another divisor."
于 2011-07-13T02:02:12.317 回答
0

这不能回答您的编码问题,但我认为值得评论。

sqrt:负数的平方根是一个虚数。Python 支持带有complex(real_part, imaginary_part).

所以你的 sqrt 计算可以写成:

elif choice == "5":
    numberA = raw_input("Enter the number you wish to find the square root of: ")
    numberA = float(numberA)
    if numberA < 0:
        sqrt_numberA = complex(0, sqrt(abs(numberA)))
    else:
        sqrt_numberA = sqrt(numberA)

    print "Your result is:", sqrt_numberA

除法:除以 0 是无限的。Python 用“inf”表示无限。事实上,它返回数字高于 1E309 的“inf”:

>>> 1E309
inf

并且这些数字中的两个相除产生'nan'

>>> 1E309/1E309
nan

这样你就可以写:

elif choice == "4":
    numberA = raw_input("Enter your dividend: ")
    numberB = raw_input("Enter your divisor: ")
    try: 
        div = division(numberA, numberB)
    except ZeroDivisionError:
        div = 'inf'

    print "The quotient of those numbers is:", div

此代码应额外扩展以返回 -inf,当 numberA 为负数时,并考虑到 0/0 等情况

于 2011-07-13T05:50:31.377 回答