如何进行循环?

时间:2019-04-27 13:23:09

标签: python python-3.x

如果用户输入的数字小于-100或大于200,则我希望程序要求他们反复“输入介于-100和200之间的数字”,直到他们这样做为止。我该怎么办?


while r < temp:
    first_year = float(input("What is the first year?"))
    average_temp = float(input("What is the average temputure for the year"))
    if average_temp < -100:
        print  ("Enter a number between -100 and 200")
    if average_temp > 200:
        print ("Enter a number between -100 and 200)
    if averaget_temp <200:
        print ("The average temputure for year" , first_year , "is" , average_temp)
    if average_temp > -100:
            print ("The average temputure for year" , first_year , "is" , average_temp)

    r += 1

3 个答案:

答案 0 :(得分:0)

您可以使用while进行循环。

condition = True
while condition:
    # do_something
    condition = False

答案 1 :(得分:0)

您可以使用while循环:

while True:
    average_temp = int(input('Number: '))
    if 200 > average_temp > -100:
        break # <----------------- See flow diagram: tutorialspoint.com/python/python_break_statement
    else:
        print('Enter a number between -100 and 200!')

答案 2 :(得分:0)

一个例子:

number = float(input("Enter a number between -100 and 200: "))
while number<-100 or number>200:
    number = float(input("Enter a number between -100 and 200: "))
print ("Thank you")