所以我试图在 python 3 中制作一个简单的二十一点游戏,一切都很好,除了我受不了。如果我输入 2 它不会做任何事情。提前致谢。编辑 1:击球效果很好。编辑 2:发布整个脚本,以便您可以重现我面临的问题,正如@roganjosh 建议的那样。
from random import shuffle
import sys
def deal(deck, player, dealer):
shuffle(deck)
for _ in range(2):
player.append(deck.pop())
dealer.append(deck.pop())
def score(hand):
non_aces = [c for c in hand if c != 'A']
aces = [c for c in hand if c == 'A']
sum = 0
for card in non_aces:
if card in 'JQK':
sum += 10
else:
sum += int(card)
for card in aces:
if sum <= 10:
sum += 11
else:
sum += 1
return sum
def display_info(player, dealer, stand):
print("Your cards: [{}] ({})".format(']['.join(player), score(player)))
if stand:
print("Dealer cards: [{}] ({})".format(']['.join(dealer), score(dealer)))
else:
print("Dealer cards: [{}] [?]".format(dealer[0]))
def results(player, dealer, hand, stand):
if score(player) == 21 and hand:
print("Blackjack! You won!")
sys.exit()
elif score(player) > 21:
print("Busted. You lost!")
sys.exit()
if stand:
if score(dealer) > 21:
print("Dealer busted. You won!")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("You lost!")
else:
print("Push. Nobody wins or losses.")
sys.exit()
def hit_stand(deck, player, dealer, hand, stand):
print("What would you like to do")
print("[1] - Hit\n[2] - Stand")
choice = input("> ")
hand = False
if choice == '1':
player.append(deck.pop())
elif choice == '2':
stand = True
while score(dealer) <= 16:
dealer.append(deck.pop())
不负相思意
幕布斯7119047
相关分类