国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > Python基础教程 > python面对用户无意义输入的解决

python面对用户无意义输入的解决

python输入

头像

小妮浅浅

2021-08-09 14:45:223308浏览 · 0收藏 · 0评论

问题

正在编写一个接受用户输入的程序。

#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18:
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

只要用户输入有意义的数据,程序就会按预期工作。

C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!

但如果用户输入无效数据,它就会失败:

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
  File "canyouvote.py", line 1, in <module>
    age = int(input("Please enter your age: "))
ValueError: invalid literal for int() with base 10: 'dickety six'

而不是崩溃,我希望程序再次要求输入。像这样:

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Sorry, I didn't understand that.
Please enter your age: 26
You are able to vote in the United States!

当输入无意义的数据时,如何让程序要求有效输入而不是崩溃?

我如何拒绝像 那样的值-1,int在这种情况下这是一个有效但无意义的值?

解决方法

完成此操作的最简单方法是将input方法放入 while 循环中。使用continue时,你会得到错误的输入,并break退出循环。

当您的输入可能引发异常时

使用try和except检测用户何时输入无法解析的数据。

while True:
    try:
        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #better try again... Return to the start of the loop
        continue
    else:
        #age was successfully parsed!
        #we're ready to exit the loop.
        break
if age >= 18:
    print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")

实现你自己的验证规则

如果要拒绝 Python 可以成功解析的值,可以添加自己的验证逻辑。

while True:
    data = input("Please enter a loud message (must be all caps): ")
    if not data.isupper():
        print("Sorry, your response was not loud enough.")
        continue
    else:
        #we're happy with the value given.
        #we're ready to exit the loop.
        break
 
while True:
    data = input("Pick an answer from A to D:")
    if data.lower() not in ('a', 'b', 'c', 'd'):
        print("Not an appropriate choice.")
    else:
        Break

结合异常处理和自定义验证

以上两种技术都可以组合成一个循环。

while True:
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        continue
 
    if age < 0:
        print("Sorry, your response must not be negative.")
        continue
    else:
        #age was successfully parsed, and we're happy with its value.
        #we're ready to exit the loop.
        break
if age >= 18:
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

以上就是python面对用户无意义输入的解决,希望对大家有所帮助。更多Python学习指路:python基础教程

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

韩漫免费漫画在线观看方法| 《温柔的大姐姐》动漫免费观看| 尼姑小寡妇免费播放电视剧| 《无法满足》意大利| 公与媳2电视剧免费观看| 爸妈离婚女儿成爸爸的女人| 男生将78申请女生的定眼| 多村三飞母姐妹花女儿的小说| 《吃你吃上瘾》| 欧美一区二区| 《法国空乘4》在线观看| 老头跪在两腿之间吃奶| 被两个男人吃奶三P| 姑妄言| 暴躁妹妹免费观看40集电视剧| 妈妈的职业4免费观看全集在线播..| 欧美寡妇性猛交XXX| 真人做爰45分钟| 第79章厨房的欢愉方晴| 《一次特殊的游泳课》| 挪威的森林截了一段3分钟| 美女两个球球抖动抓球球视频| 女性三角毛毛区有片毛掉光正常吗| 白色变态蕾丝超薄开档连身袜| 闺蜜说他老公特别大怎么回复| 白日梦我| 伦理《播种》| 女人被濯满精子后多久排卵期| B站暴躁少女CSGO| 女生给男生吃小头头| 邻里大战| 儿子找不到媳妇妈妈能以身相许吗| 7C7PCCA片| 小柔与王叔的第一次相遇是第几集 | 少女第一次第1集在线观看| 电影《满足2》完整版在线观看| 口咬的10个绝招图片| 同学的母亲中字ID| 母与子免费观看大全电视剧 | 十大名阴排名一览表| 成全高清免费观看MV