我无法在二十一点游戏中关闭我的while循环[关闭]

Posted

技术标签:

【中文标题】我无法在二十一点游戏中关闭我的while循环[关闭]【英文标题】:I cant close my while-loop in a Blackjack game [closed] 【发布时间】:2021-12-02 19:32:25 【问题描述】:

我正在用 Python 制作二十一点游戏,并且正在使用 while 循环。在我的代码中,我得到了一个带有 if 语句的输入,如果玩家选择再玩一轮(输入“Y”),游戏就会重新开始(我没有问题)。我的问题是,如果玩家输入“N”(表示否),游戏仍然会重新运行。我想做的是当玩家输入“N”时,游戏结束。我的代码有什么问题?我被卡住了,无法弄清楚。

您也可以在我的代码中看到,我在 while 循环中多次使用 closed = True 并且它有效(例如,如果玩家获得 21 分,则游戏结束)。唯一一次 closed = True 不起作用是在我的代码的elif play_again.lower() != "Y": closed = True 部分。

closed = False
while not closed:

    if player_total == 21:
        print("\nCongrats, the player got a Blackjack!")
        closed = True

    else:
        input_answer = int(input("\n1 - Hit \n2 - Stand \n \nDo you wish to Hit or Stand? "))

        if input_answer == 1:
            print("You chose to Hit.")
            give_cards(player_hand, 1)
            player_total = bjm.calculate_hand_value(player_hand)
            print(f'\nThe player has got a new card: player_hand[2]. \nThe player currently has ", ".join(player_hand),'
                  f' with a total value of player_total.')

            if player_total > 21:
                print("Busted! You got a total value over 21.")
                closed = True

        elif input_answer == 2:
            print("\nYou chose to Stand.")
            print(f'The dealers cards are: " and a ".join(dealer_hand), with a total value of dealer_total.')
            print(f'The players cards are ", ".join(player_hand) with a total value of player_total.')
            print_result(player_hand, dealer_hand)

            play_again = input("\nDo you want to play another round? Y/N: ")

            if play_again.lower() != "N":
                print("You wanted to play another round, you will be dealt two new cards.\n")

                player_hand.clear()
                dealer_hand.clear()

                give_cards(player_hand, 2)
                give_cards(dealer_hand, 2)

                print(f'\nThe players new cards are: " and a ".join(player_hand), with a total value of player_total.')
                print(f'The dealers new cards are: " and a ".join(dealer_hand), with a total value of dealer_total.')

            elif play_again.lower() != "Y":
                closed = True

closed = True 

【问题讨论】:

.lower() 的东西永远不会等于大写 Y 【参考方案1】:

比您现在使用的结束循环更好的方法是简单地将closed = True 中的条件替换为break

此外,您的代码无法正常工作的原因是您试图将 .lower()(它总是给您一个小写字母)与一个大写字母进行比较,这意味着这种情况:

            elif play_again.lower() != "Y":
                closed = True

永远不会是真的。

"y"代替"Y"

【讨论】:

非常感谢!!我想我因为阅读代码太多而失明,所以我没有注意到,但你的回答有效:) 干杯兄弟!如果您还有任何问题,请不要犹豫:)。【参考方案2】:

我不确定您为什么要使用双重否定。试试这个:

while True:
    ...
    elif play_again.lower() != "Y":
        break #instead of closed=True

【讨论】:

【参考方案3】:

删除 .lower()。您正在尝试小写并使用 Y 或 N 验证它,这永远不会到达 else 语句

例子-


play_again = input("\nDo you want to play another round? Y/N: ")

if play_again != "N":
    print("You wanted to play another round, you will be dealt two new cards.\n")

elif play_again != "Y":
    print("Dont want to play")

【讨论】:

【参考方案4】:

您可以将可接受的答案设置为

valid_answers = ['yes', 'no']
if valid_answers == "no":
   print("You wanted to play another round, you will be dealt two new cards.\n")
   player_hand.clear()
   dealer_hand.clear()
   give_cards(player_hand, 2)
   give_cards(dealer_hand, 2)
   print(f'\nThe players new cards are: " and a ".join(player_hand), with a total value of player_total.')
            print(f'The dealers new cards are: " and a ".join(dealer_hand), with a total value of dealer_total.')

else: 
   closed = True

【讨论】:

以上是关于我无法在二十一点游戏中关闭我的while循环[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

发出警报后,我无法在我的反应本机应用程序中关闭我的键盘

试图做一个二十一点游戏

如何在 iOS + Swift 中关闭我的应用程序的 Internet 访问

有没有办法修复 while 循环不使用 time.sleep 更新 tkinter 标签?

c_cpp 多人二十一点纸牌游戏

我如何将二十一点牌中的“王牌”定义为 11 或 1?