简体   繁体   English

输入验证(Python)

[英]Input validation (Python)

For input I need to have a string that contains both numbers and letters or only letters. 对于输入,我需要一个包含数字和字母或仅包含字母的字符串。 Atm my code works with these conditions, but how can I add that input must not consist of punctuation marks? Atm我的代码可以在这些条件下工作,但是如何添加不能包含标点符号的输入呢?

nimi = input("...name: ")
while name.isalnum == True or name.isnumeric():
    name = input("...name: ")

最好使用正则表达式。

if re.match(r'^(?!\d+$)[\da-zA-Z]+$', pass):

As stated above, using regex is better. 如上所述,使用regex更好。 But the below condition will work for you. 但是以下情况将为您工作。 Use string.punctuation to take count of all the punctuations. 使用string.punctuation来计算所有标点符号。

import string
while(name.isalnum() or name.isalpha()) and not any(i in string.punctuation for i in name):

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

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