Python Blackjack:如果陈述不起作用

问题描述:

嘿我正在为我的二十一点游戏中的Python添加投注,出于某种原因,现在我的代码不会先通过if语句。任何帮助将不胜感激。Python Blackjack:如果陈述不起作用

我的代码如下:

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() 
+0

请摆脱那些'全球'。 – Matthias

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

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

应该只是

play_count = total_play_count 

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