-1
我昨天早些時候發佈,但現在完全修改我的代碼(使用框架的塊從其他人的迭代,添加了一堆東西。雖然,請看看)Python 2.7 - 黑傑克IndexErrors,以及更多...
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:\Users\Owner\Downloads\Black Jack Revision.py", line 43, in <module>
showHand()
File "C:\Users\Owner\Downloads\Black Jack Revision.py", line 23, in showHand
print "The dealer is showing a %d" % dealer[0]
IndexError: list index out of range
此外,我還沒有找到一種方法讓指令循環回主菜單。我沒有讓菜單變成一個函數,因爲它變得太混亂了,局部變量不再工作。
援助將不勝感激。謝謝。
有什麼辦法解決這個新錯誤? :/ – user3211205
@ user3211205這是一個提示,'deck.pop(dealDealer)'不會刪除'dealDealer'的值,它將刪除該列表的位置。 如果經銷商= [11,12,13,14,15,16],那麼'dealer.pop(5)'將移除16.如果我們重複'dealer.pop(5)',那麼我們將得到'indexError因爲5號位不再存在。這裏是[doc](http://docs.python.org/2/tutorial/datastructures.html#more-on-lists) – Tito
對不起,沒有線索......我是新手。 – user3211205