为啥我的 while 循环中的条件在 python 中不起作用?
Posted
技术标签:
【中文标题】为啥我的 while 循环中的条件在 python 中不起作用?【英文标题】:Why aren't the conditions within my while loop working in python?为什么我的 while 循环中的条件在 python 中不起作用? 【发布时间】:2021-08-11 11:02:49 【问题描述】:我试图创建一个程序,您可以在其中与计算机玩石头剪刀布。我设法达到了基本水平,但我想升级游戏,以便您可以跟踪用户和计算机在用户决定的所有游戏中赢得了多少。
我使用了一个 while 循环,只要玩的总游戏数(不包括平局)不超过用户在开始时决定的“最佳”游戏,它就会执行并继续游戏。但它不起作用。例如,如果我决定用电脑玩 3 种游戏中最好的游戏,并且我赢了 3 次,它仍然会要求我输入石头或剪刀。
import random
# keep track of who wins throughout the game
user_wins = 0
computer_wins = 0
# list of possible inputs to play the game
options = ["r", "p", "s"]
#user chooses how many games to play on aggregate
best_of = int(input('Best out of: '))
print(best_of)
# define a function that takes in two players as arguments
# and returns True if player wins based on the standard rules
# of rock paper scissors
def is_winner(player, opponent):
if (player == 'r' and opponent == 's') or \
(player == 'p' and opponent == 'r') or \
(player == 's' and opponent == 'p'):
return True
# game continues while the number of games(not inclunding ties)
# does not exceed best_of
while (user_wins + computer_wins) <= best_of:
user_input = input("""
What do you play?
'r' for rock,
'p' for paper,
's' for scissors
Press 'q' to quit: """).lower()
# loop breaks if user enters 'q'
if user_input == "q":
break
# if user_input is not 'r', 'p', or 's', then has to try again
if user_input not in options:
continue
#computer selects 'r', 'p' or 's' randomly
computer_pick = random.choice(options)
#print each players input
print("You picked: ", user_input)
print("Computer picked: ", computer_pick)
# if inputs are the same then, game is tie and goes through loop again
if user_input == computer_pick:
print('It\'s a tie!')
continue
# if the player is winner, print a win message and add 1 to the user_wins variable
elif is_winner(user_input, computer_pick):
print('You won!')
user_wins += 1
# by default, if player doesnt win AND not a tie, the computer wins
# so prints 'you lost' and add 1 to computer_wins
else:
print("You lost!")
computer_wins += 1
#print final score once game is finshed
print("You won ", user_wins, " times.")
print("The computer won ", computer_wins, " times.")
print("Goodbye!")
【问题讨论】:
这需要调试。我推荐以下文章,尽管其中一些内容(例如启用编译器警告)与 Python 不太相关:ericlippert.com/2014/03/05/how-to-debug-small-programs 【参考方案1】:while (user_wins + computer_wins) <= best_of
在上面的行中,您检查了
while (user_wins + computer_wins) < best_of
【讨论】:
【参考方案2】:看看这个 - 问题在于 while 循环:
while (user_wins + computer_wins) <= best_of:
如果用户选择 3,您忘记了 user_wins 和 computer_wins 都从 0 开始这一事实——因此,它将循环 4 次。
因此,你想要的效果很简单:
while (user_wins + computer_wins) < best_of:
【讨论】:
【参考方案3】:欢迎来到 ***! ? 错误在while
循环条件中
您的情况是:
while (user_wins + computer_wins) <= best_of:
当正确时
while user_wins + computer_wins < best_of:
-
我删除了无用的括号(检查运算符优先级here)
条件为
while user_wins + computer_wins < best_of
。当您运行while
循环时,它会检查条件然后运行代码。所以,在你的情况下,如果user_wins + computer_wins == best_of
应该会中断。因此,您应该使用<
而不是<=
最后,我用PEP8 重新格式化了您的代码。你可以在网上做同样的事情here。
【讨论】:
以上是关于为啥我的 while 循环中的条件在 python 中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章