python - 不能禁止用户名中包含字母字符的数字
问题描述
我正在尝试编写一个代码,禁止新用户在他的用户名中写入数字。
到目前为止,我已经写了这个:
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 禁止同时包含数字和普通字符的用户名。
在此先感谢您的任何建议:)
解决方案
isnumeric()
如果整个字符串是数字,则返回 true。您想知道是否有任何字符是数字。
any(x.isnumeric() for x in new_user)
推荐阅读
- javascript - 如何使用 useEffect 更好地设置 fetch?
- javascript - 反应流渲染器缩放
- c - ncurses中具有透明度的重叠窗口
- windows - 汇编程序中printf函数的使用
- java - 在 android studio 中,StringRequest 可以连接到互联网,但不能连接到具有 IP 地址的本地主机?
- r - 将 rdrobust 库中的 rdplot 转换为 ggplot
- c# - 我有 3 行的 SQLite db,当我从行中删除数据时,数据会丢失但行仍然会丢失,我怎样才能删除那个空白行?
- gstreamer - 使用 gstreamer 校正镜头畸变
- android - 与 ViewPager2.registerOnPageChangeCallback 一起使用时,Android ImageSwitcher 未设置初始图像
- dataframe - 如何在某些条件下在 Spark DataFrame 中创建新列“count”