【发布时间】: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_again和draw_again?您只能将这些变量用于用户的输入。 -
如果你希望能够多次玩游戏,你应该使用循环。
-
这真的是你的 coe 的正确缩进吗?即使用户回答
n“你想玩吗?”,你也会进入while draw_again == 'hit':循环。 -
欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。如果您的问题只是循环游戏,那么“内部”不应该出现在帖子中。
标签: python