好吧,我正在为一个项目做这个,每当我试图让它除以零或平方根时,程序就会关闭一个负数。我试图找到可以插入代码中的内容,使其显示一条消息,然后再次提示输入该值,但是我尝试插入的所有内容都会导致程序在我启动时立即关闭。
这是没有插入任何东西来修复崩溃的计算器。
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,程序就会崩溃。有没有办法让它回到它所在的行?
此外,对于关闭程序,它应该显示的消息无法读取,因为程序在显示后立即关闭,或者正在同时执行命令。无论哪种方式,都无法阅读该消息。有什么方法可以使消息出现在单独的对话框窗口中,从而导致程序在窗口关闭时关闭?或者至少有一种方法可以让它在关闭前延迟?
如果我有任何术语错误,请纠正我。我对此还是有些陌生。
而且,与往常一样,对程序任何部分的(建设性)反馈总是值得赞赏的。