简体   繁体   中英

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?

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. But the below condition will work for you. Use string.punctuation to take count of all the punctuations.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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