我正在进行心脏游戏编程,但我对如何计算每张卡中的所有分数感到困惑 这是有价值的卡,其他卡有0值
>>> card_value ={"QS":13,"2H":1,"3H":1,"4H":1,"5H":1,"6H":1,"7H":1,"8H":1,"9H":1,"0H":1,"JH":1,"QH":1,"KH":1,"AH":1,"0D": -10}
但是如果一组列表中的所有子列表都包含一个心脏,那么得分将为-26,如果一组列表中的所有子列表都包含一个心脏和10个钻石(“0D”),则得分将为是-36
例如
>>> list1 = [['QD', '7D', '5H', '0D'], ['7C', '6D', '2H', 'KS'], ['8S', '3D', '9C', 'KD'], ['4D', '9D', 'KH', '4C'],['7S', 'JH', '4H', 'QC'], ['2S', 'KC', 'AD', 'AC'], ['JD', '8D', 'QS', '6H'], ['AH', '9H', '9S', '6S'],['5S', '0S', 'JC', '2D'],['5D', '8H', '0C', '7H'], ['JS', '6C', '2C', 'AS'], ['3S', '4S', '5C', '0H'], ['3C', 'QH', '3H', '8C']]
此输出将为-36
>>> list2 = ["5C","QS","JH"]
此输出将为14
这是我到目前为止所尝试的:
def score(list,score=0):
for cards in list:
if cards in card_value.keys():
score += card_value[cards]
if cards == "H":
score = -36
if cards == "H" or "0D":
score = -26
return score
答案 0 :(得分:2)
在两种情况下都将total
设置为零,然后执行类似
for i in list2: total=total+card_value.get(i,0)
for l in list1:
for i in l:
total=total+card_value.get(i,0)
从list1返回的总数是16而不是-36这种技术我认为你必须得到这个想法
编辑: 现在有了这个“如果列表中的所有牌都有心,那么得分将是-26”商业。我在下面的评论中假设这实际上意味着“如果一组列表中的所有子列表包含至少一个心脏,则分数将为-26”
for l in list1:
for i in l:
total=total+card_value.get(i,0)
if i[0] == 'H': # first character of card string is a H- so it's a heart
heart_counter = heart_counter + 1
if heart_counter == len(list1):
total = total - 26