我无法在二十一点游戏中关闭我的 while 循环
I cant close my while-loop in a Blackjack game
我正在 Python 制作 Blackjack 游戏,我正在使用 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
我不确定你为什么要使用双重否定。试试这个:
while True:
...
elif play_again.lower() != "Y":
break #instead of closed=True
比您现在使用的结束循环更好的方法是简单地将 closed = True
中的条件替换为 break
。
此外,您的代码不工作的原因是因为您试图将 .lower()(它总是给您一个小写字母)与一个大写字母进行比较,这意味着这种情况:
elif play_again.lower() != "Y":
closed = True
永远不会是真的。
将其替换为 "y"
而不是 "Y"
移除.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")
您可以将可接受的答案设置为
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
我正在 Python 制作 Blackjack 游戏,我正在使用 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
我不确定你为什么要使用双重否定。试试这个:
while True:
...
elif play_again.lower() != "Y":
break #instead of closed=True
比您现在使用的结束循环更好的方法是简单地将 closed = True
中的条件替换为 break
。
此外,您的代码不工作的原因是因为您试图将 .lower()(它总是给您一个小写字母)与一个大写字母进行比较,这意味着这种情况:
elif play_again.lower() != "Y":
closed = True
永远不会是真的。
将其替换为 "y"
而不是 "Y"
移除.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")
您可以将可接受的答案设置为
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