所以基本上我是在给自己制造一些有趣的东西,我遇到了一个问题。我有一个不能放入用户名字符串的非法项目列表。基本上,如果有人输入了一个用户名,并且它在变量illegal_items中包含了一些内容,那么就不应该让他们输入这个用户名。问题是,我不能让它正常工作。这是我尝试过的方法。
username = "roboticperson"
illegal_items = ['test', 'robotic', 'emphatic']
if any(illegal_items in s for s in username):
print("Your username cannot contain any illegal items.")
else:
print("Hello, "+username+"!")
它应该说:
Your username cannot contain any illegal items.
它说的是:
TypeError: 'in <string>' requires string as left operand, not list
如果这是另一个帖子的副本,我很抱歉。
编辑:我正在尝试这样做,这样变量illegal_items中的任何内容都会将用户名标记为无效。
发布于 2020-05-02 09:20:15
username中的for s正在逐个字符遍历用户名。因此,您实际上正在做的是:
test在r中吗?
test在o中吗?
改为执行以下操作:
username = "roboticperson"
illegal_items = ['test', 'robotic', 'emphatic']
if any( s in username for s in illegal_items):
print("Your username cannot contain any illegal items.")
else:
print("Hello, "+username+"!")
这将检查是否
测试是否在机器人人中? //false
机器人在机器人人中是机器人吗? //true
https://stackoverflow.com/questions/61553213
复制相似问题