【发布时间】:2015-08-19 15:38:07
【问题描述】:
无论 user_hand 中有什么卡片,我的返回值总是打印出总共 20。有什么理由吗?
suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
deck = [(suit, rank) for rank in ranks for suit in suits]
random.shuffle(deck,random.random)
user_hand = []
dealer_hand = []
user_hand.append(deck.pop())
dealer_hand.append(deck.pop())
user_hand.append(deck.pop())
dealer_hand.append(deck.pop())
def handtotal (hand):
total = 0
for rank in hand:
if rank == "J" or "Q" or "K":
total += 10
elif rank == 'A' and total < 11:
total += 11
elif rank == 'A' and total >= 11:
total += 1
elif rank == '2':
total += 2
elif rank == '3':
total += 3
elif rank == '4':
total += 4
elif rank == '5':
total += 5
elif rank == '6':
total += 6
elif rank == '7':
total += 7
elif rank == '8':
total += 8
elif rank == '9':
total += 9
return total
print ("Your current hand is {}".format(user_hand))
print ("This provides you with a total of:")
print (handtotal(user_hand))
【问题讨论】:
-
rank == "J" or "Q" or "K"会返回真,你想做rank in ["J" , "Q" , "K"] -
您确实应该努力将错误减少到尽可能少的代码。这会让你知道哪一行是不正确的。
-
您还需要在分配值之前对排名进行排序。
A, J, J应该是 21,而不是 31。J, A, A应该是 12,而不是 22,但这有点复杂。
标签: python function printing return return-value