简体   繁体   English

在Python中验证输入

[英]Validating an input in Python

I have a piece of code that asks for an input. 我有一段代码要求输入。 The input must not contain any numbers: 输入内容不得包含任何数字:

def main():

    invalid = []
    foo = ""


    foo = str(input("Enter a word: ") )

    invalid = ["1","2","3","4","5","6","7","8","9","0"]

    for eachInvalid in invalid:
        if eachInvalid in foo:
            print("Not a real word!")
            main()
        else:
            pass


main()

So far, it works but I am not proud of it. 到目前为止,它是可行的,但我并不为此感到骄傲。 What would be the better way of doing this be? 这样做的更好方法是什么? For example, a word does not contain things like ~@: so I would have to add those to that list. 例如,一个单词不包含〜@:之类的东西,因此我必须将其添加到该列表中。 How would I avoid this but keep the code rather clean and readable? 我将如何避免这种情况,但使代码保持清晰和可读性?

Also, another error, if I run the code and type something in like hello, that is valid but if I type something invalid it takes me back to the start but when I type a valid word it still says invalid? 另外,另一个错误是,如果我运行代码并键入类似hello的内容,那是有效的,但是如果我键入无效的内容,则会使我回到开始的位置,但是当我键入有效的单词时,仍然表示无效? For example: 例如:

Enter a word: hello
>>> ================================ RESTART ================================
>>> 
Enter a word: 123
Not a real word!
Enter a word: hello
Not a real word!

How would I fix this error and what would be the best way of looking for invalid characters in an input? 如何解决此错误?在输入中查找无效字符的最佳方法是什么?

edit: nevermind, regular expression is fine. 编辑:没关系,正则表达式很好。

While a more complex validation is an appropriate use case for a regular expression, in this simple case there is a built in function isalpha() which checks whether a string only contains alphabetic characters. 虽然更复杂的验证是正则表达式的适当用例,但在这种简单情况下,有一个内置函数isalpha()可以检查字符串是否仅包含字母字符。

foo.isalpha()

Returns True or False . 返回TrueFalse

Note that in Python 3 this will deal with all unicode characters defined as "letters". 请注意,在Python 3中,这将处理所有定义为“字母”的unicode字符。

You could use a simpler method to test for a digit in the whole string, rather than testing each letter individually. 您可以使用一种更简单的方法来测试整个字符串中的数字,而不是分别测试每个字母。

if any(x.isdigit() for x in foo):
        print("Invalid Word")
    else:
        print("valid word")

You could reverse the way you are doing, instead of having a list of invalids, it is better to have a list of valids. 您可以颠倒您的操作方式,而不是拥有无效列表,而最好拥有有效列表。 Then check every character of your string : 然后检查字符串中的每个字符:

valids = ['a', 'b', 'c', 'd']
for letter in foo:
    if (not letter in valids):
            print("Not a real word!")

It is even easier with regex as it is easier to list all valids options : 使用regex甚至更容易,因为它更容易列出所有有效选项:

import re
if (not re.match("^[a-zA-Z_ ]*$", foo)):
            print("Not a real word!")

The regex ^[a-zA-Z_ ]*$ meaning a word that contain only symbols in [a-zA-Z_ ] 正则表达式^[a-zA-Z_ ]*$表示仅包含[a-zA-Z_ ]符号的单词

If you want to stay with a list of invalids, use negated regular expression : 如果要保留无效列表,请使用否定的正则表达式:

if (re.match("[^0-9@]", foo)):
            print("Not a real word!")

where [^0-9@] means anything but the characters defined between the brackets 其中[^0-9@]表示除括号之间定义的字符外的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM