gpt4 book ai didi

Python 计算器除以零/对 Neg 求平方。诠释。程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:52 26 4
gpt4 key购买 nike

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

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

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)

关于Python 计算器除以零/对 Neg 求平方。诠释。程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6673247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com