If 语句始终为真(字符串)
If Statement Always True (String)
我这里有一个相当简单的剪刀石头布程序,我在使用 if 语句时遇到了一些问题。出于某种原因,当我输入剪刀石头布(真值)时,程序总是执行
if 'rock' or 'paper' or 'scissors' not in player:
print("That is not how you play rock, paper, scissors!")
出于某种原因。完整程序如下
computer = ['rock', 'paper', 'scissors']
com_num = randint(0,2)
com_sel = computer[com_num]
player = input('Rock, paper, scissors GO! ')
player = player.lower()
if 'rock' or 'paper' or 'scissors' not in player:
print("That is not how you play rock, paper, scissors!")
if player == 'rock' or 'paper' or 'scissors':
#win
if player == 'rock' and com_sel == 'scissors':
print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
if player == "paper" and com_sel == "rock":
print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
if player == 'scissors' and com_sel == 'paper':
print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
#draw
if player == com_sel:
print('It\'s a draw!')
#lose
if player == 'rock' and com_sel == 'paper':
print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
if player == 'paper' and com_sel == 'scissors':
print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
if player == 'scissors' and com_sel == 'rock':
print('You lose.', com_sel.title(), "beats", player, '!', sep = '')```
if
中的条件错误。
考虑带括号的 if
语句:
if ('rock') or ('paper') or ('scissors' not in player):
它将始终 return True
因为 rock
将始终为真。
您需要交换条件的操作数
if player not in computer:
交换后,这一行变得无关紧要(而且它的条件也是错误的)你需要删除它:
if player == 'rock' or 'paper' or 'scissors':
查看此页面,其中包含 Python 的运算符优先级(它们应用的顺序):
https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
您可以看到 not in
的排名高于 or
,这意味着它首先被评估。因此,您可以将 if 语句重写为:
if 'rock' or 'paper' or ('scissors' not in player): ...
现在我们看到您确实 or
三件事。字符串不为空,因此第一个 'rock'
的计算结果已经为真,因此整个事情始终为真。
要分解您的陈述,
if 'rock' or 'paper' or 'scissors' not in player:
假设,player = 'scissors'
。此条件将被评估为,
('rock'
) 或 ('paper'
) 或 ('scissors' not in player
)
再次评估为,
True or True or False
因此总是评估为 True
因为字符串(例如 'rock')总是评估为 True 并忽略其他因为 OR
(任何一个 True 为 True)。所以无论你在播放器中放什么都没有关系。
正确的条件
if player not in ['rock', 'paper', 'scissors']:
此语句检查 player
是否不在给定列表中。
if player not in {'rock', 'paper', 'scissors'}:
print("That is not how you play rock, paper, scissors!")
...
我这里有一个相当简单的剪刀石头布程序,我在使用 if 语句时遇到了一些问题。出于某种原因,当我输入剪刀石头布(真值)时,程序总是执行
if 'rock' or 'paper' or 'scissors' not in player:
print("That is not how you play rock, paper, scissors!")
出于某种原因。完整程序如下
computer = ['rock', 'paper', 'scissors']
com_num = randint(0,2)
com_sel = computer[com_num]
player = input('Rock, paper, scissors GO! ')
player = player.lower()
if 'rock' or 'paper' or 'scissors' not in player:
print("That is not how you play rock, paper, scissors!")
if player == 'rock' or 'paper' or 'scissors':
#win
if player == 'rock' and com_sel == 'scissors':
print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
if player == "paper" and com_sel == "rock":
print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
if player == 'scissors' and com_sel == 'paper':
print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
#draw
if player == com_sel:
print('It\'s a draw!')
#lose
if player == 'rock' and com_sel == 'paper':
print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
if player == 'paper' and com_sel == 'scissors':
print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
if player == 'scissors' and com_sel == 'rock':
print('You lose.', com_sel.title(), "beats", player, '!', sep = '')```
if
中的条件错误。
考虑带括号的 if
语句:
if ('rock') or ('paper') or ('scissors' not in player):
它将始终 return True
因为 rock
将始终为真。
您需要交换条件的操作数
if player not in computer:
交换后,这一行变得无关紧要(而且它的条件也是错误的)你需要删除它:
if player == 'rock' or 'paper' or 'scissors':
查看此页面,其中包含 Python 的运算符优先级(它们应用的顺序):
https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
您可以看到 not in
的排名高于 or
,这意味着它首先被评估。因此,您可以将 if 语句重写为:
if 'rock' or 'paper' or ('scissors' not in player): ...
现在我们看到您确实 or
三件事。字符串不为空,因此第一个 'rock'
的计算结果已经为真,因此整个事情始终为真。
要分解您的陈述,
if 'rock' or 'paper' or 'scissors' not in player:
假设,player = 'scissors'
。此条件将被评估为,
('rock'
) 或 ('paper'
) 或 ('scissors' not in player
)
再次评估为,
True or True or False
因此总是评估为 True
因为字符串(例如 'rock')总是评估为 True 并忽略其他因为 OR
(任何一个 True 为 True)。所以无论你在播放器中放什么都没有关系。
正确的条件
if player not in ['rock', 'paper', 'scissors']:
此语句检查 player
是否不在给定列表中。
if player not in {'rock', 'paper', 'scissors'}:
print("That is not how you play rock, paper, scissors!")
...