fqas = None
while fqas not in ("yes", "no", "Yes", "No"):
fqas = input(">>> [yes/no]: ")
if fqas.lower() == "yes":
print("\nAwesome! Current pay: $", base_pay + 50)
elif fqas.lower() == "no":
fqas_no = int(input("if not, how much do you think she deserve? \n>>> "))
if 50 <= fqas_no :
print("AMAZINGGGG! current pay: $", base_pay + fqas_no)
continue
elif fqas_no <= 50 :
print("That's cool, current pay: $", base_pay + fqas_no)
continue
except ValueError:
print("Numbers only please")
如何添加最后一个循环来检查fqas_no
下的整数?我已检查范围,但无法获取整数。尝试使用SyntaxError
时收到无效的ValueError
。
除了:
之外,该代码均正常运行 except ValueError:
print("Numbers only please")
答案 0 :(得分:0)
看看documentation有关异常处理的信息。您需要包含一个try
语句:
try:
if 50 <= fqas_no :
#etc
except ValueError:
print("Numbers only please")
这两个在一起。仅使用except
是错误的语法。
旁注,您应该在输入本身上调用lower()
。这样,您的代码就会更干净:
while fqas not in ["yes", "no"]:
fqas = input(">>> (yes/no): ").lower()
if fqas == "yes":
#etc
答案 1 :(得分:0)
您已经知道如何循环直到获得有效(或无效)输入。要检测整数,您可以尝试将输入转换为int
try:
value = int(fqas_no)
except:
# loop around to try again
更好,请使用内置方法
if fqas_no.isdigit():
答案 2 :(得分:0)
您可能会遇到问题,这是您的问题。
base_pay = 1000
fqas = None
while fqas not in ("yes", "no", "Yes", "No"):
fqas = input(">>> [yes/no]: ")
if fqas.lower() == "yes":
print("\nAwesome! Current pay: $", base_pay + 50)
elif fqas.lower() == "no":
try:
fqas_no = int(input("if not, how much do you think she deserve? \n>>>"))
except ValueError:
print('Numbers only please')
break
if 50 <= fqas_no:
print("AMAZINGGGG! current pay: $", base_pay + fqas_no)
continue
elif fqas_no <= 50:
print("That's cool, current pay: $", base_pay + fqas_no)
continue
这应该可以解决您的问题。