【问题标题】:While loop user input in range在范围内循环用户输入
【发布时间】:2023-04-02 02:24:01
【问题描述】:

我有一些代码,我想向用户询问 1-100 之间的数字,如果他们在这些之间输入一个数字,它将打印 (Size: (input)) 并打破循环,但如果他们输入它会打印一个 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.")

【问题讨论】:

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


【解决方案1】:

应该这样做。

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)

【讨论】:

  • 非常感谢!完美运行
【解决方案2】:

你甚至从来没有进入你的循环。

c=100
while c<100:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2012-10-13
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多