python - 如何正确编写 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':
解决方案
推荐阅读
- javascript - Mongoose Model.find() 方法在生产环境中被破坏的问题
- android - 在 Google Firebase 中,我们如何检索从 Firebase 控制台上传的下载 URL 列表和图像?
- sql - 将 from 子句的 table 用于 where 子句的 select 语句的利弊是什么?
- javascript - Ajax xmlhttprequest 缺少“Access-Control-Allow-Origin”标头
- php - Codeigniter 记住我的自动完成功能 = 'new-password'
- angular - 在另一个html文件Angular 5中使用组件选择器后页面停止加载
- swift - UIView 的圆角曲线边缘
- nativescript - 在 NativeScript 中访问 Android 包时出现问题
- r - 如何在数据框中创建列表
- windows - 下载 MQseries server 7.0 版 32 位 windows 版