![](/img/trans.png)
[英]Python 3: How can I display what cards the dealer and players have in my game of black jack?
[英]I'm currently writing a black jack game and I am having problems with passing my code onto the "calculating_scores" function
任何人都可以帮助我解决我的问题,让我的代码传递到计算分数 function 上,而我的代码不想在每次我在another_card = input("Type 'y' if you'd like another card:\n")
def play():
global game_continue
user_cards = []
dealer_cards = []
for _ in range(2):
user_cards.append(deal_cards())
dealer_cards.append(deal_cards())
total_user = sum(user_cards)
print(f"You got the cards: {user_cards}, giving you a total of {total_user}\n")
print(f"The dealer got the card: {dealer_cards[0]}\n")
while game_continue:
another_card = input("Type 'y' if you'd like another card:\n")
if another_card == "yes" or another_card == "y":
user_cards.append(deal_cards())
new_user_total = sum(user_cards)
print(f"You have the cards, {user_cards}, giving you a total of: {new_user_total}")
dealer_total = dealer_cards[0] + dealer_cards[1]
print(f"The dealers second card was {dealer_cards[1]}, giving him a total of {dealer_total}")
calculating_scores(card_list=user_cards)
elif another_card == "no" or another_card == "n":
game_continue = False
dealer_total = dealer_cards[0] + dealer_cards[1]
print(f"The second dealers card was a {dealer_cards[1]} giving him a total of {dealer_total}")
calculating_scores(card_list=user_cards)
else:
print("Sorry this is an invalid option!")
# 10:16pm altering the arguments called after calculating_score to make the program properly function
def calculating_scores(card_list):
if sum(card_list) == 21:
return "Dealer won blackjack!"
if 11 in card_list and sum(card_list) > 21:
card_list.remove(11)
card_list.append(1)
return sum(card_list)
不清楚你在问什么,但有几点注意事项:
您调用calculating_scores
但忽略它的返回值。
calculating_scores
分数应该只计算分数 - 不确定是否有赢家。
应该有2个循环。 一个给用户,直到他们的分数大于 21 或者他们说不再有卡片。 还有一个给庄家——他们应该拿牌直到他们的分数> = 17。
我只想让play()
返回分数。 然后在别处打印获胜者信息。
像这样的东西:
def play():
user_cards = [deal_cards(), deal_cards()]
dealer_cards = [deal_cards(), deal_cards()]
user_total = calculating_scores(user_cards)
print(f"You got the cards: {user_cards}, giving you a total of {user_total}\n")
print(f"The dealer got the card: {dealer_cards[0]}\n")
while user_total < 21:
another_card = input("Type 'y' if you'd like another card:\n")
if another_card == "yes" or another_card == "y":
user_cards.append(deal_cards())
user_total = calculating_scores(user_cards)
print(f"You have the cards, {user_cards}, giving you a total of: {user_total}")
else:
break
dealer_total = calculating_scores(dealer_cards)
print(f"The dealers second card was {dealer_cards[-1]}, giving him a total of {dealer_total}")
while dealer_total <= 17:
dealer_cards.append(deal_cards())
dealer_total = calculating_scores(dealer_cards)
print(f"The dealers next card was {dealer_cards[-1]}, giving him a total of {dealer_total}")
return (user_total, dealer_total)
def calculating_scores(card_list):
while 11 in card_list and sum(card_list) > 21:
card_list.remove(11)
card_list.append(1)
return sum(card_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.