While范围内的用户输入 [英] While loop user input in range

查看:98
本文介绍了While范围内的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码要问用户输入1-100之间的数字,如果他们在这些数字之间输入数字,则会打印(大小:(输入))并中断循环,但是,如果他们输入1-100以外的数字将打印(大小:(输入)),然后继续向他们询问一个数字,但是我遇到了一些问题.

I have some code that I want to ask the user for a number between 1-100 and if they put a number between these it will print (Size: (input)) and break the loop, if however, they put in a number outside 1-100 it will print (Size: (input)), and proceed to re-ask them for a number, I have run into some problems though.

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.")

推荐答案

这应该做到.

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)

这篇关于While范围内的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆