首页 > 解决方案 > 不能禁止用户名中包含字母字符的数字

问题描述

我正在尝试编写一个代码,禁止新用户在他的用户名中写入数字。

到目前为止,我已经写了这个:

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 禁止同时包含数字和普通字符的用户名。

在此先感谢您的任何建议:)

标签: pythonlistnumbers

解决方案


isnumeric()如果整个字符串是数字,则返回 true。您想知道是否有任何字符是数字。

any(x.isnumeric() for x in new_user)

推荐阅读