假设hashtag可以在2到3之间的其他位置。
hashtag = '#'
game = '12#3'
if "all values of game, besides hashtag are 1, 2 or 3":
print(‘yes’)
else:
print(‘no’)
如何在Python中编写if语句(现在写为引号之间的伪代码(?
这就是您想要的吗?
hashtag = '#'
game = '12#3'
if hashtag not in game:
print('yes')
else:
print('no')
只需执行以下操作:
hashtag = '#'
game = '12#3'
if (v in game) and (v != hashtag):
print('yes')
else:
print('no')
您的任务也可以这样表述:如果game
仅包含集合{1,2,3,#}
的字符,则打印"是"。
allowed_characters = {'1', '2', '#', '3'}
game = '12#3'
check = all(x in allowed_characters for x in game)
如果满足该条件,上面的代码片段将在检查中存储True
。
更多示例:
>>> game = '1#23'
>>> all(x in allowed_characters for x in game)
True
>>> game = 'a123'
>>> all(x in allowed_characters for x in game)
False