如何限制srj im在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")
答案 0 :(得分:0)
由于您在theN
/ try
内,如果数据不正确,您可以简单地except
raise
。
ValueError
答案 1 :(得分:-2)
将if语句if srj > 1 or srj < 0:gen0()
放在break
行之前
:^)