我有这个代码,只运行一次。谁能帮我把这段特殊的代码循环起来无限期地运行呢,先谢谢了。
这是我的代码。
def get_non_negative_int(prompt):
while True:
try:
value = int(input(prompt))
except ValueError:
print("Sorry, I didn't understand that.")
continue
if value < 0:
print("Sorry, your response must not be negative.")
continue
else:
break
return value
age = get_non_negative_int("Please enter your age: ")
kids = get_non_negative_int("Please enter the number of children you have: ")
如果你要求你的程序在输入正值后再次加载,那么你只需要在程序中添加一个 while
循环。
while(True):
age = get_non_negative_int("Please enter your age: ")
kids = get_non_negative_int("Please enter the number of children you have: ")
这将无限期地运行。