Python 这里怎么写?try:除ZeroDivision之外错误:print“;你不能被零除&引用;
帮助,如何避免除法错误Python 这里怎么写?try:除ZeroDivision之外错误:print“;你不能被零除&引用;,python,calculator,Python,Calculator,帮助,如何避免除法错误 what = input ("+\n-\n/\n*\nЧто делаем?:") a = float(input("\nВведите первое число:")) b= float(input("Введите второе число:")) if what == "+": c = a + b print("\nРезультат:" + str
what = input ("+\n-\n/\n*\nЧто делаем?:")
a = float(input("\nВведите первое число:"))
b= float(input("Введите второе число:"))
if what == "+":
c = a + b
print("\nРезультат:" + str(c))
elif what == "/":
c = a / b
print("\nРезультат:" + str(c))
elif what == "*":
c = a * b
print("\nРезультат:" + str(c))
elif what == "-":
c = a - b
print("\nРезультат:" + str(c))
else:
print ("\nНеизвестный символ.")
input()
在哪里插入?
尝试:
除了零除错误:打印“不能被零除!”您可以这样做
what = input ("+\n-\n/\n*\nЧто делаем?:")
a = float(input("\nВведите первое число:"))
b= float(input("Введите второе число:"))
if what == "+":
c = a + b
print("\nРезультат:" + str(c))
elif what == "/":
try:
c = a / b
print("\nРезультат:" + str(c))
except ZeroDivisionError:
print("Can't divide by zero!")
elif what == "*":
c = a * b
print("\nРезультат:" + str(c))
elif what == "-":
c = a - b
print("\nРезультат:" + str(c))
else:
print ("\nНеизвестный символ.")
input()
有分歧的地方。更妙的是,只需检查
b!=0
而不是在:elif what='/':if(b!=0):c=a/b中添加“try…except…”而使用try块测试部分代码是否存在最终异常,并使用except管理任何可能发生的异常。OHHH!!我修好了…谢谢!