while循环用户输入范围

时间:2013-04-18 02:53:17

标签: python loops python-3.x while-loop

我有一些代码,我想问用户一个1-100之间的数字,如果他们在这些之间放一个数字,它将打印(大小:(输入))并打破循环,如果他们放入它会打印出一个1-100之外的数字(大小:(输入)),并继续重新询问它们的数字,但我遇到了一些问题。

c=100
while c<100:
    c=input("Size: ")
    if c == 1 or 2 or 3:
        print ("Size: "+c)
        break
    else:
        print ("Size: "+c)
        print ("Invalid input. Try again.")

2 个答案:

答案 0 :(得分:3)

这应该这样做。

c=input("Size: ")
while int(c)>100 or int(c)<1: 
    #Will repeat until the number is between 1 and 100, inclusively.
    #It will skip the loop if the value is between 1 and 100.

    print ("Size: "+c)
    print ("Invalid input. Try again.")
    c=input("Size: ")

#once the loop is completed, the code following the loop will run
print ("Size: "+c)

答案 1 :(得分:1)

你甚至都没有进入你的循环。

c=100
while c<100:

c以100开始,while检查是否小于100。

相关问题