首页 > 解决方案 > 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')

标签: pythonwhile-loop

解决方案


第一个循环只有在同时等于、和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.


推荐阅读