我的二十一点游戏代码有什么问题?

时间:2019-04-17 17:16:02

标签: python

该代码通常运行良好,但是,在尝试再次执行play命令后,我使它退出了程序并且未按预期循环。导致问题的代码如下。

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")

我希望游戏能够不断循环直到被告知不这样做。实际输出会导致游戏在玩了两轮后关闭。

1 个答案:

答案 0 :(得分:2)

这种代码的结构方式,直到用户选择退出之前,没有一个主要的外部循环一直持续下去。 (但是您显然没有发布整个程序,所以也许有这样一个循环,您没有向我们展示?)

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

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

也许是这样的:

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? ')