我正在检查以确保动物园可以让船只与动物一起旅行,为了做到这一点,由于安全原因,离动物园最近的人必须至少在60米以外。我使用下面的代码
#Ask them how far away the closest person is from the ship
while True:
try:
Distance = int(input("How far away is the closest person
from the ship? (in meters) "))
except ValueError:
print("Please enter a number")
continue
else:
break
if Distance <=60:
print("Please ask them to move away")
else:
print("People are at a safe distance")
我想这样做,以便问题重新询问他们何时放入太低的距离,然后他们可以再次检查并放入新的距离 我希望它看起来像:
How far away is the closest person from the ship (in meters)? 8
Please ask them to move away
How far away is the closest person from the ship (in meters)? 789
People are at a safe distance
答案 0 :(得分:0)
将ask()
变为函数。从无限循环中调用它。如果break
是&gt;,则循环仅ask()
(停止调用Distance
) 60。
def ask():
while True:
try:
Distance = int(input("How far away is the closest person from the ship? (in meters) "))
except ValueError:
print("Please enter a number")
continue
else:
break
return Distance
while True:
if ask() <=60:
print("Please ask them to move away")
else:
print("People are at a safe distance")
break