while True:
try:
name=str(input("Enter your name="))
age=int(input("Enter your age="))
except ValueError:
print("error!! please enter the values again")
continue
else:
break
current_year=int(input("What is the current year you are living in="))
n=100-age
x=n+current_year
print(name,"",x,"is the year you will turn 100")
那么,如果他/她为年龄输入一个负数,那么我如何创建一个错误消息供我使用,从而允许用户重新输入年龄
我会尝试使用while循环
我们一直在计算用户的年龄,直到他们输入高于0的值
添加一个
while
循环,询问年龄是否小于零。如果满足此条件(用户输入负数),循环将继续(它将要求新的输入):相关问题 更多 >
编程相关推荐