Python Blackjack:如果声明不起作用

时间:2013-04-13 22:03:08

标签: python if-statement python-3.x blackjack

嘿我正在努力为我的二十一点游戏添加投注,因为某些原因我的代码现在不会先通过if语句。任何帮助将不胜感激。

我的代码如下:

def play(): 
       """ Function that involves all gameplay and allows the user to play blackjack. All 
   other functions are to support the gameplay while this function covers the actual 
   gameplay. """

   #initializing a counter for how many times the user has played
   global total_play_count

   #total winnings of a player if they already played from last hand
   global total_winnings 

   if total_play_count == 0:
        play_count = 0
   else: 
        play_count = total_play_count

    #setting a variable to hold the users total cash
    if play_count == 0: 
        winnings = 100
    else: 
        winnings = total_winnings

    #dealing user and dealer their cards
    global user_show_cards 
    global dealer_show_cards

    user_show_cards = [card(), card()]
    dealer_show_cards = [card(), card()]

    #declaring total variables
    global user_total 
    global dealer_total

    user_total = 0  
    dealer_total = 0

    #lists to hold numerical values of the hand
    global user_total_cards 
    global dealer_total_cards

    user_total_cards = [] 
    dealer_total_cards = []

    #initializing the values of cards and storing them in the lists above
    user_total_cards = ready_for_total(user_show_cards, user_total_cards, user_total)
    dealer_total_cards = ready_for_total(dealer_show_cards, dealer_total_cards, dealer_total) 

    #getting initial player totals
    user_total = sum(user_total_cards)
    dealer_total = sum(dealer_total_cards)

    #printing what is initially shown on screen
    print "" 
    print "Let's play Blackjack" 
    print "Note: Dealer stays on ALL 17s"
    print "" 
    if play_count == 0: 
        print "You start with $100"
    else: 
        pass
    bet = int(raw_input("Enter your bet: "))
    winnings -= bet
    print ""
    print "You now have $%s" %(winnings)
    print ""
    print "Your cards are %s %s" %(str(user_show_cards[0]), str(user_show_cards[1]))
    print "The dealer's up card is a(n) %s" %(str(dealer_show_cards[0]))
  

代码不会通过上述行

        #first condition to see if the player has Blackjack 
    if user_total == 21: 
        print "Blackjack! You Win!"
        bet1 = bet * 2
        print "You receive $%s" %(bet1) 
        winnings += bet + bet1
        print "Your winnings now total $%s" %(str(winnings))

    elif dealer_total == 21: 
        print "Dealer got Blackjack. You lose."
        print "" 
        print "You now have $%s" %(winnings)
    else: 
        total_winnings = winnings 
        return total_winnings
        hit_or_stay()  


    print ""
    play_again = raw_input("Play again? (y/n): ") 

    if play_again == "y": 
        total_winnings = winnings 
        play_count += 1
        total_play_count = play_count
        play() 
        return total_play_count 
        return total_winnings
    else: 
        print "" 
        print "Your winnings totaled $%s" %(winnings)
        print "Goodbye, come back soon!"

play()  

1 个答案:

答案 0 :(得分:1)

看起来if语句下的所有代码都是缩进的,我认为这不是你想要的。另外我认为如果声明对你没有任何帮助。

if total_play_count == 0:
        play_count = 0
   else: 
        play_count = total_play_count

应该只是

play_count = total_play_count

也不要使用全局变量。请不要。