import random
SUITS = ("\u2660", "\u2665", "\u2666", "\u2663")
PIPS = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K", "Q")
deck = []
player_hand = []
def create_deck():
for suit in SUITS:
for pip in PIPS:
card = (pip + suit)
deck.append(card)
print()
我认为这是这些功能中的任何一个,但我是一个平庸的人 在这种东西
def deal_card():
card = random.choice(deck)
deck.remove(card)
return card
def create_hand(hand):
for i in range(2):
deal = deal_card()
player_hand.append(deal)
def print_hand(hand):
for pip, suit in hand:
print(pip + suit,end=" ")
print()
另外,如果我正确计算王牌,我不确定我是不是
def sum_hand(hand):
total = 0
for pip,suit in player_hand:
if pip == "J" or pip == "K" or pip == "Q":
total += 10
elif pip == "A":
total += 0
else:
total += int(pip)
for pip,suit in player_hand:
if pip == "A":
total += 11
elif total > 21:
total += 1
else:
total += int(pip)
return total
def player_hit(hand):
total = sum_hand(player_hand)
choice == input("would you like to hit or stand(h/s)? ")
while choice.lower == "h":
if total < 21:
player_hand.append(deal_card())
print_hand(player_hand)
play_again = True
while play_again:
deck = []
player_hand = []
total = sum_hand(player_hand)
create_hand(player_hand)
print_hand(player_hand)
if total < 21:
player_hit(player_hand)
elif total == 21:
print("Winner!")
else:
print("bust!")
again = input("Would you like to play again(y/n)?")
if again.lower == "y":
play_again
else:
play_again = False
input("\nPress enter to exit")
以下是我得到的所有错误
我反复不断收到这些错误
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py", line 255, in choice
i = self._randbelow(len(seq))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py", line 232, in _randbelow
r = getrandbits(k) # 0 <= r < 2**k
ValueError: number of bits must be greater than zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sicarius/Desktop/Intro to programming/program9.py", line 81, in <module>
create_hand(player_hand)
File "/Users/sicarius/Desktop/Intro to programming/program9.py", line 33, in create_hand
deal = deal_card()
File "/Users/sicarius/Desktop/Intro to programming/program9.py", line 26, in deal_card
card = random.choice(deck)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py", line 257, in choice
raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence
非常感谢任何帮助
答案 0 :(得分:-1)
在您拨打deal_card()
时,看起来甲板是空的(即仍然等于[])。如果您在致电create_deck()
之前致电deal_card()
,那么您应该没问题。