python - 我的二十一点游戏代码有什么问题?
问题描述
该代码运行良好,但是,在再次尝试播放命令后,我让它退出程序并且没有按预期循环。导致问题的代码如下。
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'
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? ')
推荐阅读
- reactjs - 我可以将 React js 用于长时间运行的应用程序吗
- reactjs - Formik 复选框在检查时造成如此多的延迟 #1901
- c - 挣扎于 C 字符串和 printf %s
- c# - 无法在 C# 中发送电子邮件
- ios - 如何设置约束,如果视图的宽度不能至少为某个宽度,则该视图的宽度为 0
- angular - angular 8 populate 从 observable 下拉
- c# - 如何使用 Asp.Net Core 3.0.1 从 Windows 读取剪贴板数据
- java - 如何将json字符串转换为对象java
- regex - 如何使用正则表达式 python 在下面的字符串中获取名称“Gokila”?
- javascript - 如果 XHR 对象已经形成,则从 XHR 获取 formData 对象