python面对用户无意义输入问题怎么解决
                                            这篇文章主要介绍了python面对用户无意义输入问题怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python面对用户无意义输入问题怎么解决文章都会有所收获,下面我们一起来看看吧。

创新互联公司专业为企业提供云安网站建设、云安做网站、云安网站设计、云安网站制作等企业网站建设、网页设计与制作、云安企业网站模板建站服务,十年云安做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
问题
正在编写一个接受用户输入的程序。
#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, inage = 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面对用户无意义输入问题怎么解决
文章分享:http://www.cqwzjz.cn/article/piepph.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 