在Python中验证用户输入字符串 [英] Validating user input strings in Python

查看:194
本文介绍了在Python中验证用户输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我几乎搜索了单词"string","python","validate","user input"等的每个排列,但是我还没有找到一种适用于我的解决方案

So I've searched just about every permutation of the words "string", "python", "validate", "user input", and so on, but I've yet to come across a solution that's working for me.

我的目标是提示用户是否要使用字符串"yes"和"no"进行另一笔交易,我认为字符串比较在Python中是一个相当简单的过程,但是有些事情却并非如此.工作不正确.我使用的是Python 3.X,据我所知,输入应使用字符串而不使用原始输入.

My goal is to prompt the user on whether or not they want to start another transaction using the strings "yes" and "no", and I figured that string comparison would be a fairly easy process in Python, but something just isn't working right. I am using Python 3.X, so input should be taking in a string without using raw input, as far as I understand.

即使输入"yes"或"no",该程序也会始终取消无效输入,但真正奇怪的是,每次我输入一个字符串> 4个字符或一个int值时,它都会对其进行检查作为有效的正输入,然后重新启动程序.我还没有找到获取有效负输入的方法.

The program will always kick back invalid input, even when entering 'yes' or 'no', but the really weird thing is that every time I enter a a string > 4 characters in length or an int value, it will check it as valid positive input and restart the program. I have not found a way to get valid negative input.

endProgram = 0;
while endProgram != 1:

    #Prompt for a new transaction
    userInput = input("Would you like to start a new transaction?: ");
    userInput = userInput.lower();

    #Validate input
    while userInput in ['yes', 'no']:
        print ("Invalid input. Please try again.")
        userInput = input("Would you like to start a new transaction?: ")
        userInput = userInput.lower()

    if userInput == 'yes':
        endProgram = 0
    if userInput == 'no':
        endProgram = 1

我也尝试过

while userInput != 'yes' or userInput != 'no':

非常感谢您不仅可以解决我的问题,而且如果有人对Python如何处理字符串有任何更多的了解,那将是非常棒的.

I would greatly appreciate not only help with my problem, but if anyone has any additional information on how Python handles strings that would be great.

抱歉,如果有人已经问过这样的问题,但是我会尽力进行搜索.

Sorry in advance if someone else has already asked a question like this, but I did my best to search.

谢谢!

〜戴夫

推荐答案

您正在测试用户输入的yes还是no.添加not:

You are testing if the user input is yes or no. Add a not:

while userInput not in ['yes', 'no']:

如此快速且更接近您的意图,请使用一组:

Ever so slightly faster and closer to your intent, use a set:

while userInput not in {'yes', 'no'}:

您使用的是userInput in ['yes', 'no'],如果userInput等于'yes''no',则为True.

What you used is userInput in ['yes', 'no'], which is True if userInput is either equal to 'yes' or 'no'.

接下来,使用布尔值设置endProgram:

Next, use a boolean to set endProgram:

endProgram = userInput == 'no'

因为您已经确认userInputyesno,所以无需再次测试yesno来设置标志变量.

Because you already verified that userInput is either yes or no, there is no need to test for yes or no again to set your flag variable.

这篇关于在Python中验证用户输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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