【发布时间】:2020-09-19 22:04:45
【问题描述】:
我正在尝试编写一个代码来禁止新用户在其用户名中写入数字。
到目前为止,我一直在写这篇文章:
all_users = ['andrew', 'carolina', 'david']
new_user = "please provide a username"
if new_user.isnumeric():
print(f"the username {new_user} is unavailable, please don't use numbers.")
elif new_user not in all_users:
print(f"{new_user.title()}, welcome to the game!")
问题是在我尝试将字母字符与数字混合之前,代码工作得很好。我似乎无法找到一种方法来告诉 Python 禁止同时包含数字和普通字符的用户名。
提前感谢您的任何建议:)
【问题讨论】:
-
.isalpha()返回真,如果字符串只包含字母(也包括各种语言的 unicode 字母)