首页 > 解决方案 > 如何正确编写 OR 表达式

问题描述

我有一个非常简单的程序,它只检查用户的输入并最终要求正确的输入。

player1 = input('Scissors (S), Rock (R), Paper (P) ?:')

while player1 is not 'S' or 'R' or 'P':
 player1 = input('Invalid Input, please choose correctly! /n Scissors (S), Rock (R), Paper (P) ?:')

如果 player1 是“S”或“R”或“P”,那么它仍然要求正确输入。我不确定我错过了什么。如果这听起来很微不足道,请原谅。

编辑:

以这种方式编写仍然无法识别正确的输入

   while player1 is not 'S' or player1 is not 'R' or player1 is not 'P':
   while player1 in {'S', 'R', 'P'}:

这种方式只是识别 'S' 是正确的,如果类型 'R' 仍然是错误的

while player1 != 'S' and 'R' and 'P':

正确的:

while player1 != 'S' and player1 != 'R' and player1 != 'P':

标签: pythonlogical-operators

解决方案


推荐阅读