Python 2.7 - Black Jack IndexErrors, and more...



我昨天早些时候发布了,但现在完全修改了我的代码(使用其他人迭代中的框架块,并添加了一堆东西。不过遇到了一些问题。看一看。

import random as r
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]*4
dealer = []
player = []
decision = "y"
player_money = 5000
dealer_money = 5000
pot = 0
total = 0
def showHand():
hand = 0
for i in player: hand += i #Tally up the total
print "The dealer is showing a %d" % dealer[0]
print "Your hand totals: %d (%s)" % (hand, player)
print "You have $", player_money
print "The dealer has $", dealer_money
print "The pot right now is $", pot
print "*** *** ***"
def setup():
for i in range(2):
dealDealer = deck[r.randint(1, len(deck)-1)]
dealPlayer = deck[r.randint(1, len(deck)-1)]
dealer.append(dealDealer)
player.append(dealPlayer)
deck.pop(dealDealer)
deck.pop(dealPlayer)
menu = raw_input("Welcome to Black Jack! What would you like to do? n a) Start a new game n b) Rules n c) Quit").lower()
if menu == 'a':
setup()
while decision != 'q':
showHand()
decision = raw_input(" [B]et [H]it [S]tand [Q]uit: ").lower()
#set up betting system, as well as enemy AI
if decision == 'b':
bet = input("How much will you bet? ($)")
player_money = player_money - bet
pot = pot + bet
total = total + bet
print "*** *** ***"
print "You bet $", bet, "!"
hand = 0
for i in dealer: hand += i
if hand > 1:
dealer_bet = bet
dealer_money = dealer_money - dealer_bet
pot = pot + dealer_bet
print "*** *** ***"
print "The dealer calls your bet!"
print "*** *** ***"
total = total + dealer_bet
elif dealer_money <= bet:
dealer_bet = dealer_money
print "*** *** ***"
print "The dealer calls your bet!"
print "*** *** ***"
pot = pot + dealer_bet
total = total + dealer_bet

elif hand > 14:
dealer_bet = bet + 1.25*bet
dealer_money = dealer_money - dealer_bet
pot = pot + dealer_bet
total = total + dealer_bet
print "*** *** ***"
print "The dealer raises your bet to $", dealer_bet, "!"
print "*** *** ***"
elif dealer_money < bet:
dealer_bet = dealer_money
print "*** *** ***"
print "The dealer goes all in!"
print "*** *** ***"
pot = pot + dealer_bet
total = total+ dealer_bet

elif hand > 20:
dealer_bet = bet + 1.5*bet
dealer_money = dealer_money - dealer_bet
pot = pot + dealer_bet
total = total + dealer_bet
print "*** *** ***"
print "The dealer raises $", dealer_bet, "!"
print "*** *** ***"
elif dealer_money < 1.5*bet:
dealer_bet = dealer_money
pot = pot + dealer_bet
total = total + dealer_bet
print "*** *** ***"
print "The dealer went all in!"
print "*** *** ***"

#set up hit system, as well as enemy AI for if they will hit/stand
if decision == 'h':
dealPlayer = deck[r.randint(1, len(deck)-1)]
player.append(dealPlayer)
deck.pop(dealPlayer)
hand = 0
print "You take a hit! You received a", dealPlayer
for i in dealer: hand += i
if hand < 15:   #Dealer AI.
dealDealer = deck[r.randint(1, len(deck)-1)]
dealer.append(dealDealer)
deck.pop(dealDealer)
print "Opponent takes a hit!"
print "*** *** ***"
elif hand >= 15:   #Dealer AI.
print "Opponent stands. Mans are playing it safe."
print "*** *** ***"
hand = 0
for i in player: hand += i
if hand > 21:
print "*** *** ***"
print "Bust! You went over 21."
print "*** *** ***"
player = []     #Clear player hand
dealer = []     #Clear dealer's hand
setup()         #Run the setup again
pot = 0
dealer_money += total
total = 0
hand = 0
for i in dealer: hand +=i
if hand > 21:
print "*** *** ***"
print "Dealer Busts! You win $", total, "!"
print "*** *** ***"
player = []
dealer = []
setup()
pot = 0
player_money += total
total = 0

elif decision == 's':
dealerHand = 0           #Dealer's hand total
playerHand = 0           #Player's hand total
for i in dealer: dealerHand += i
for i in player: playerHand += i
#If player hand is greater than dealer's hand, they win.
if playerHand > dealerHand:
print "*** *** ***"
print "You had a higher total than the dealer! You just won mon!"
print "You earned $", total, "!"
print "*** *** ***"
player = []
setup()
pot = 0
player_money += total
total = 0
else:
print "*** *** ***"
print "The dealer had a higher total! You just lost mon!"
print "*** *** ***"
dealer = []
player = []
pot = 0
dealer_money += total
total = 0
if player_money == 0:
print "You're broke! GG!"
print "*** *** ***"
break
elif dealer_money <= 0:
print "Dealer is broke! You won bro!!!"
print "*** *** ***"
break
elif menu == 'b':
choice = raw_input("Welcome to Black Jack, a famous card game in the casinos. n The goal is to have a higher card sum than the dealer. n Players are given two cards at the beginning of each round. n The player can then choose to bet money, hit (receive another card), or stand (will end the round, tally up the player and dealer's card sums, and determine the winner of the round). n Remember that if your card sum exceeds 21, you will automatically lose the round! You cannot go over 21. n If you or the dealer run out of money, the game will end. n Would you like to start a game now? (y/n)")
if choice == 'y':
menu == 'a'
elif choice == 'n':
menu == 'a'
elif menu == 'c':
decision = 'q'
print "Goodbye~"

两个主要问题。有时,当我站立并且回合结束时,我会收到此错误消息。

Traceback (most recent call last):
File "C:UsersOwnerDownloadsBlack Jack Revision.py", line 43, in <module>
showHand()
File "C:UsersOwnerDownloadsBlack Jack Revision.py", line 23, in showHand
print "The dealer is showing a %d" % dealer[0]
IndexError: list index out of range

另外,我还没有找到让说明循环回主菜单的方法。我没有使菜单成为函数,因为它弄乱了太多,局部变量不再起作用。

如能提供协助,将不胜感激。谢谢。

您是否忘记在decision == 's'的 else 语句中使用setup()? 当dealer返回到showHand()时,它将为空,这会尝试访问dealer[0]

else:
print "*** *** ***"
print "The dealer had a higher total! You just lost mon!"
print "*** *** ***"
dealer = []
player = []
#here
pot = 0
dealer_money += total
total = 0

添加该更改后,我点击了几次支架("S"),您的错误没有重复。但是,在击中几次后确实发生了新错误。

Traceback (most recent call last):
File "SO.py", line 168, in <module>
setup()
File "SO.py", line 27, in setup
deck.pop(dealDealer)
IndexError: pop index out of range

在任何情况下都不会重新初始化甲板。

最新更新