每当我测试我的计算器,看看它如何处理输入不是数字,它会标记一个ValueError。更确切地说,这一个“ValueError:无法将字符串转换为浮点数:'a'”。我试图修改这个,所以有一个解决方案来处理非整数,但无济于事......非常感谢帮助。我的初学python项目(计算器)有输入问题
这里是我到目前为止的代码:
print("1. ADDITION ")
print("2. MULTIPLICATION ")
print("3. TAKEAWAY ")
print("4. DIVISION ")
Operator = int(input("please enter one of the numbers above to decide the operator you want. "))
while Operator > 4:
print("Please select a number from the options above. ")
Operator = int(input("please enter one of the numbers above to decide the operator you want. "))
if Operator != (1 or 2 or 3 or 4):
print("please enter one of the options above. ")
Operator = int(input("please enter one of the numbers above to decide the operator you want. "))
continue
while True:
Num1 = float(input("Please enter first number. "))
if Num1 == float(Num1):
print("thanks! ")
elif Num1 != float(Num1):
Num1 = float(input("Please enter first number. "))
break
while True:
Num2 = float(input("Please enter second number. "))
if Num2 == float(Num2):
print("thanks ")
elif Num1 != float(Num1):
Num1 = float(input("Please enter first number. "))
break
您需要查看['try/except'](https://docs.python.org/3/tutorial/errors.html#handling-exceptions)块。 –
...并在这里的解决方案将告诉你如何:http://stackoverflow.com/questions/8420143/valueerror-could-not-convert-string-to-float-id –