好吧,我正在为一个项目做这个,每当我试图将它除以零或平方根时,程序关闭的负数。我试图找到一些我可以插入到代码中的内容,使其显示一条消息,然后再次提示输入值,但是我尝试插入的所有内容都会导致程序在启动时立即关闭。
这是没有插入任何东西来修复崩溃的计算器。
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,程序就会崩溃。有没有办法让它回到它所在的那条线上?
此外,为了关闭程序,由于程序在显示后立即关闭,或者命令正在同时执行,因此无法读取它应显示的消息。无论哪种方式,都无法读取消息。有没有办法让消息出现在一个单独的对话窗口中,当窗口关闭时会导致程序关闭?或者至少是一种让它在关闭之前延迟的方法?
如果我的术语有误,请纠正我。我还是有点新鲜。
而且,一如既往,(建设性的)对程序任何部分的反馈总是受到赞赏。
答案 0 :(得分: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)
答案 1 :(得分: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
干杯!
答案 2 :(得分: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."
答案 3 :(得分: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."
答案 4 :(得分:0)
这不能解答您的编码问题,但我认为值得评论。
sqrt :负数的平方根是一个虚数。 Python supports complex numbers与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
当numberA为负时,此代码应另外扩展为返回-inf,并考虑诸如0/0
等情况