4 回答
TA贡献1883条经验 获得超3个赞
您的第一个if陈述的问题是,您评估了 、 等的真值"1","2"它们都是真的。
您必须in card每次重复:
def cardval(card):
if "2" in card or "3" in card or "4" in card: # etc.
return int(re.findall("\d+",card)[0])
# etc.
TA贡献1943条经验 获得超7个赞
你应该改变:
if "2" or "3" or ... in card:
至:
if "2" in card or "3" in card or ...:
等等
TA贡献1829条经验 获得超4个赞
您可以执行以下操作:
def cardval(card):
card_digit = re.findall("\d+",card)
if card_digit:
return card_digit[0]
elif "Ace" in card:
return 1
else:
return 10
TA贡献1877条经验 获得超1个赞
作为其他答案的替代方案,您还可以执行以下操作:
>>> card = ['Diamond_Ace', 'Diamonds_1', 'Diamonds_Jack']
>>> if any(i.endswith(('2', '3', '4', '5', '6', '7', '8', '9', '10')) for i in card):
... print('Found it')
...
>>> if any(i.endswith(('Jack', 'Queen', 'King')) for i in card):
... print('Found it')
...
Found it
>>> if any(i.endswith('Ace') for i in card):
... print('Found it')
...
Found it
添加回答
举报