此问题在此处已有答案:
Asking the user for input until they give a valid response(22个答案)
昨天关闭。
print("ax^2 + bx + c = 0")
def ask_a():
a = int(input("""Please enter a:
a = """))
if a == 0:
print("Please input the correct number! \n")
a = int(input("""Please enter a:
a = """))
else:
try:
a == int(a)
print(f"a = {a}")
except ValueError:
print("Please input the correct number! \n")
a = int(input("""Please enter a:
a = """))
a = ask_a()
错误消息:
Traceback (most recent call last):
File "c:\Users\jbtua\OneDrive\Desktop\wut is this\Personal Folder\Programming Projects\Python\blank.py", line 19, in <module>
a = ask_a()
File "c:\Users\jbtua\OneDrive\Desktop\wut is this\Personal Folder\Programming Projects\Python\blank.py", line 4, in ask_a
a = int(input("""Please enter a:
ValueError: invalid literal for int() with base 10: 'a'
我仍然不知道这是什么原因。
1条答案
按热度按时间pcww981p1#
要处理异常,您不希望在
a = input("""Please enter a: a = """)
步骤生成错误更好的方法: