Python 在查询中跳过了While循环;未报告任何错误
尽管没有报告错误,但此代码仍有问题。程序只是跳过两个while循环。有人能帮我吗Python 在查询中跳过了While循环;未报告任何错误,python,while-loop,Python,While Loop,尽管没有报告错误,但此代码仍有问题。程序只是跳过两个while循环。有人能帮我吗 def play_game(): #print(logo) user_cards = [] computer_cards = [] is_game_over = False for _ in range(2): user_cards.append(deal_card()) #deal_card() is predifined function computer_cards.ap
def play_game():
#print(logo)
user_cards = []
computer_cards = []
is_game_over = False
for _ in range(2):
user_cards.append(deal_card()) #deal_card() is predifined function
computer_cards.append(deal_card())
user_score = calculate_score(user_cards)
computer_score = calculate_score(computer_cards) #calculate score is predefined function
print(f" Your cards: {user_cards}, current score: {user_score}")
print(f" Computer's first card: {computer_cards[0]}")
if user_score == 0 or computer_score == 0 or user_score > 21:
is_game_over = True
else:
while is_game_over:
user_should_deal = input("Type 'y' to get another card, type 'n' to pass: ")
if user_should_deal == "y":
user_cards.append(deal_card())
else:
is_game_over = True
while computer_score != 0 and computer_score < 17:
computer_cards.append(deal_card())
computer_score = calculate_score(computer_cards)
print(f" Your final hand: {user_cards}, final score: {user_score}")
print(f" Computer's final hand: {computer_cards}, final score: {computer_score}")
print(compare(user_score, computer_score))
while input("Do you want to play a game of Blackjack? Type 'y' or 'n': ") == "y":
clear()
play_game()
def play_game():
#印刷品(标志)
用户卡=[]
计算机卡=[]
游戏结束了吗
对于范围(2)内的uu:
append(deal_card())#deal_card()是预定义的函数
计算机卡。附加(交易卡())
用户评分=计算评分(用户卡)
计算机评分=计算评分(计算机卡)#计算评分是预定义的功能
打印(f“您的卡:{user\u cards},当前分数:{user\u score}”)
打印(f“计算机的第一张卡:{Computer_cards[0]}”)
如果用户得分=0或计算机得分=0或用户得分>21:
游戏结束了吗
其他:
当游戏结束时:
用户\u应该\u deal=input(“键入“y”获得另一张卡,键入“n”通过:”)
如果用户\u应该\u处理==“y”:
用户卡。追加(交易卡())
其他:
游戏结束了吗
当电脑记分时!=0分,计算机评分<17分:
计算机卡。附加(交易卡())
计算机评分=计算评分(计算机卡)
打印(f“您的最后一手牌:{user\u cards},最终分数:{user\u score}”)
打印(f“计算机的最后一手:{计算机卡},最终分数:{计算机卡}”)
打印(比较(用户分数、计算机分数))
while input(“您想玩21点游戏吗?键入'y'或'n':”)==“y”:
清除()
玩游戏
您应该在while条件之前添加一个not
关键字,因为它现在声明它将在游戏结束时继续运行,并且您的变量开始时为false,因此它现在甚至不会输入一次
固定while循环:
while not is_game_over:
user_should_deal = input("Type 'y' to get another card, type 'n' to pass: ")
if user_should_deal == "y":
user_cards.append(deal_card())
else:
is_game_over = True
while computer_score != 0 and computer_score < 17:
computer_cards.append(deal_card())
computer_score = calculate_score(computer_cards)
游戏尚未结束时:
用户\u应该\u deal=input(“键入“y”获得另一张卡,键入“n”通过:”)
如果用户\u应该\u处理==“y”:
用户卡。追加(交易卡())
其他:
游戏结束了吗
当电脑记分时!=0分,计算机评分<17分:
计算机卡。附加(交易卡())
计算机评分=计算评分(计算机卡)
当游戏结束时:
,现在这个循环只有在游戏结束时才会运行,这肯定不是你想要的。代码可能有其他问题,但请尝试添加而不是。谢谢,它现在可以工作了。谢谢您和@Dan Simon,它工作得更好。