如何限制while循环中的输入需要认真研究

时间:2016-09-11 19:23:08

标签: python while-loop

如何限制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")

2 个答案:

答案 0 :(得分:0)

由于您在theN / try内,如果数据不正确,您可以简单地except raise

ValueError

答案 1 :(得分:-2)

将if语句if srj > 1 or srj < 0:gen0()放在break行之前 :^)