如何将srj的输入限制在1和0之间,然后重新启动整个程序
def gen0(): # input all the initial data
while True: # this will loop infinitely if needed
try: # this will try to take a float from the user
j=int(input('how many juveniles are there in generation 0? '))
srj=float(input('what is the survival rate of juveniles '))
break # if the user gives a number then break the loop
except ValueError: #if the user does not give a number then the user will be told to retry
print("Sorry, there was an error. Please enter a positive number")
因为您是在
try
/except
中中断,所以如果数据不正确,您可以简化raise
aValueError
。你知道吗将if语句
if srj > 1 or srj < 0:gen0()
放在break
行之前:^)
相关问题 更多 >
编程相关推荐