首页 > 解决方案 > 我的二十一点游戏代码有什么问题?

问题描述

该代码运行良好,但是,在再次尝试播放命令后,我让它退出程序并且没有按预期循环。导致问题的代码如下。

play_again = 'y' or 'n'

draw_again = 'hit' or 'hold'

print("This is a simple game of blackjack. The main objective is to stay under or equal to 21 in value.")
print("Number cards are worth their number. Face cards are worth 11. Aces are worth either 1 or 11")
print("If you want to draw again type hit. To finish type hold.")


play_game = input("would you like to play? (y/n):")
if play_game == 'y':
    shuffleDeck()
    print ("your starting hand is")
    drawFaceUp()
    drawFaceUp()
    draw_again = input("hit, or hold (hit/hold):")

while draw_again == 'hit':
    print("your next card is")
    drawFaceUp()
    draw_again = input("hit, or hold (hit/hold):")

if draw_again == 'hold':
    score = input("Enter your score <score number>:")

if score <= '15':
    print("good job, but try and get a little closer to 21 next time")
    play_again = input("Play again? (y/n):")

    if play_again == 'y':
        newDeck()
        shuffleDeck()
        print ("your starting hand is")
        drawFaceUp()
        drawFaceUp()
        draw_again = input("hit, or hold (hit/hold):")

        while draw_again == 'hit':
            print("your next card is")
            drawFaceUp()
            draw_again = input("hit, or hold (hit/hold):")

        if draw_again == 'hold':
            score = input("Enter your score <score number>:")            

    elif play_again == 'n':
        print ("end of program")

elif score > '15' and score < '21':
    print("Nice. you should test you luck again.")
    play_again = input("Play again? (y/n):")

    if play_again == 'y':
        newDeck()
        shuffleDeck()
        print ("your starting hand is")
        drawFaceUp()
        drawFaceUp()
        draw_again = input("hit, or hold (hit/hold):")

        while draw_again == 'hit':
            print("your next card is")
            drawFaceUp()
            draw_again = input("hit, or hold (hit/hold):")

        if draw_again == 'hold':
            score = input("Enter your score <score number>:")            

    elif play_again == 'n':
        print("end of program")

elif score == '21':
    print("you got a perfect score. see if you can do it again.")
    play_again = input("Play again? (y/n):")

    if play_again == 'y':
        newDeck()
        shuffleDeck()
        print ("your starting hand is")
        drawFaceUp()
        drawFaceUp()
        draw_again = input("hit, or hold (hit/hold):")

        while draw_again == 'hit':
            print("your next card is")
            drawFaceUp()
            draw_again = input("hit, or hold (hit/hold):")

        if draw_again == 'hold':
            score = input("Enter your score <score number>:")        

    elif play_again == 'n':
        print("end of program")

elif play_game == 'n':
    print("end of program")

我希望游戏能够无休止地循环,直到被告知不要。实际输出导致游戏在 2 轮游戏后关闭。

标签: python

解决方案


这段代码的结构方式是,在用户选择退出之前,没有一个主外循环会一直运行。(但你显然没有发布整个程序,所以也许有这样一个循环你没有向我们展示?)

相反,用户玩一个游戏,根据他们的分数看到一条消息,然后可以选择再玩一个游戏,仅此而已。

您可能想要重组代码,以便所有这些都发生在一个循环中。这也将消除大量的代码重复。

也许是这样的:

play_again = 'y'
while play_again == 'y':
    # play the game
    if score <= 15:
        print 'try harder'
    elif score <= 20:
        print 'not bad'
    elif score == 21:
        print 'great'
    play_again = input('Play again? ')

推荐阅读