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

查看:27
本文介绍了在 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.

即使输入是"或否",程序也会始终踢回无效输入,但真正奇怪的是,每次我输入长度超过 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.

谢谢大家!

~戴夫

推荐答案

您正在测试用户输入是 is 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 等于 True,则为 Truecode>'yes' 或 'no'.

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,所以无需测试 yes> 或 no 再次设置您的标志变量.

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天全站免登陆