抱歉,这是一个非常新手的问题。
我正在尝试制作一个石头剪刀布程序。
我想等待用户做出选择 r、p、s。
我不知道为什么我似乎无法让它工作。
当我运行我的程序时,无论我输入什么,它都不会退出第一个 while 循环。我不明白为什么当我输入eather 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')
答案 0 :(得分:3)
只有当 while
同时等于 p1_choice
、'r'
和 'p'
时,第一个 's'
循环才会停止,这是不可能的。>
p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p'
评估为 True
如果p1_choice
不等于'r'
或 如果p1_choice
不等于'p'
< strong>or 如果 p1_choice
不等于 's'
。
我相信您打算使用 and
而不是 or
。
答案 1 :(得分:2)
@codewelldev 已经很好地指出在你的代码中不可能跳出 while 循环。 由于在 OR 条件中,如果任何条件为 True,循环将继续进行。您将输入以下内容之一。 'r'、's'、'p'。所以因为你在否定,最多有两个 True(s) 和一个 False。它永远不会完成。 可能您想使用“不在”功能。
p1_choice, p2_choice = None, None
while p1_choice not in ('r','s','p'):
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
while p2_choice not in ('r','s','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')
答案 2 :(得分:1)
这是另一种方式,我认为最容易阅读:
def getPlayerChoice() -> str:
choice = '';
while not (choice == 'r' or choice == 's' or choice == 'p'):
choice = input('Please make your choice. Rock(r), Scissors(s), Paper(p): ')
p1_choice = getPlayerChoice();
p2_choice = getPlayerChoice();
答案 3 :(得分:0)
这个语句 p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
始终为 True,因此不会中断循环
我强烈建议您按如下方式运行代码:
while True:
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
if p1_choice == 'r' or p1_choice == 's' or p1_choice == 'p':
break
else:
continue
while True:
p2_choice = input('Player two please make your choice. Rock(r), Scissors(s), Paper(p): ')
if p2_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
continue
else:
break
可读性更强,更不容易出错。
另一个改进是,将用户输入的检查委托给一个函数:
def check_inputs(user_input):
if user_input == 'r' or user_input == 's' or user_input == 'p':
print('noe')
return True
else:
print('fuckl')
return False
while True:
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
check = check_inputs(p1_choice)
if check:
break
else:
continue
这跟在 DRY Rule
之后