python - Python无限循环检查条件是否满足
问题描述
对不起,如果这是一个非常新手的问题。
我正在尝试制作一个石头、纸、剪刀的程序。
我想等待用户做出选择 r, p, s。
我不知道为什么我似乎无法让它工作。
当我运行我的程序时,无论我输入什么,它都不会退出第一个 while 循环。我不明白为什么当我输入 r、p 或 s 时它不继续?
while p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
while p2_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
p2_choice = input('Player two please make your choice. Rock(r), Scissors(s), Paper(p): ')
print()
if p1_choice == 'r' and p2_choice == 'p':
print('Player two wins')
elif p1_choice == 'r' and p2_choice == 's':
print('Player one wins')
elif p1_choice == 's' and p2_choice == 'r':
print('Player two wins')
elif p1_choice == 's' and p2_choice == 'p':
print('Player one wins')
elif p1_choice == 'p' and p2_choice == 'r':
print('Player one wins')
elif p1_choice == 'p' and p2_choice == 's':
print('Player two wins')
else:
print('The game is a tie')
解决方案
第一个循环只有在同时等于、和while
时才会停止,这是不可能的。p1_choice
'r'
'p'
's'
p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p'
评估为True
ifp1_choice
不等于'r'
或ifp1_choice
不等于'p'
或ifp1_choice
不等于's'
。
我相信你的意思是使用and
而不是or
.
推荐阅读
- javascript - stopImmediatePropagation on mat-expansion-panel not working after upgrading to Angular 9
- java - FCM Listview but I can not list
- arrays - if 语句中的错误数组下标
- python - 使用 argparse 库从 unix 中的命令行收集输入文件
- javascript - Material-UI @page 规则?
- laravel - 405(方法不允许)Axios - Laravel
- python - “元素 <> 无法滚动到视图中”错误对通常的解决方案没有响应
- jupyter-notebook - 保存文件时控制 HTML 文件的大小
- django - 对多个数据库配置的困惑
- html - 如何在节点js中获取文件路径并传递给数据库请求