我试着做一个计算器作为家庭作业。如果我提供正确的输入,它看起来工作正常。但如果我将第一个数字设为空字符串,程序就会崩溃:
TypeError: 'NoneType' object is not subscriptable
为什么会发生这种情况,我该如何解决?
def read_numbers():
try:
number1 = float(input("Give first number: "))
number2 = float(input("Give the second number: "))
return [number1,number2]
except ValueError:
read_numbers()
except TypeError:
read_numbers()
def summa():
numbers = read_numbers()
return numbers[0]+numbers[1]
command = ""
while command != "q":
command = input("Give command: ")
if command == "s":
print(summa())
elif command == "q":
break