【问题标题】:What is wrong with my Blackjack game code? [duplicate]我的二十一点游戏代码有什么问题? [复制]
【发布时间】:2019-04-17 17:16:01
【问题描述】:

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

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 轮后关闭。

【问题讨论】:

  • play_again = 'y' or 'n'play_again = 'y' 相同。
  • 为什么要在脚本开头分配变量play_againdraw_again?您只能将这些变量用于用户的输入。
  • 如果你希望能够多次玩游戏,你应该使用循环。
  • 这真的是你的 coe 的正确缩进吗?即使用户回答n“你想玩吗?”,你也会进入while draw_again == 'hit':循环。
  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。如果您的问题只是循环游戏,那么“内部”不应该出现在帖子中。

标签: python


【解决方案1】:

这段代码的结构方式是,在用户选择退出之前,不会有一个主外循环一直运行。 (但你明明没有把整个程序贴出来,所以可能有这样一个循环你没有给我们展示?)

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

您可能希望重新构建代码,以便它在一个循环中完成。这也将消除大量的代码重复。

大概是这样的:

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

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 2016-06-21
    • 2020-03-06
    • 1970-01-01
    • 2011-02-04
    • 2023-03-21
    • 2020-05-18
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多