Python while循环或条件
在第二个while循环中,我被卡住了,永远也出不去。如何修复Python while循环或条件,python,while-loop,logical-operators,Python,While Loop,Logical Operators,在第二个while循环中,我被卡住了,永远也出不去。如何修复 def playGame(wordList): new_hand = {} choice = input('Enter n to deal a new hand, or e to end game: ') while choice != 'e': if choice == 'n': player_or_pc = input('Enter u to have yourself play, c to ha
def playGame(wordList):
new_hand = {}
choice = input('Enter n to deal a new hand, or e to end game: ')
while choice != 'e':
if choice == 'n':
player_or_pc = input('Enter u to have yourself play, c to have the computer play: ')
while player_or_pc != 'u' or player_or_pc != 'c':
print('Invalid command.')
player_or_pc = input('Enter u to have yourself play, c to have the computer play: ')
if player_or_pc == 'u':
print('player played the game')
elif player_or_pc == 'c':
print('PC played the game')
choice = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
else:
print('Invalid command.')
choice = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
播放器或pc!='u'或播放器或pc!='c'
始终为真:
- 如果
为player\u或
,则不等于'u'
,因此这两个条件之一为真'c'
- 如果
为player\u或
,则不等于'c'
,因此这两个条件之一为真'u'
- 两个条件都为真的任何其他值
和:
while player_or_pc != 'u' and player_or_pc != 'c':
或者使用=
,将整个内容放在括号中,并在前面使用而不是:
while not (player_or_pc == 'u' or player_or_pc == 'c'):
在这一点上,使用成员资格测试更为清晰:
while player_or_pc not in {'u', 'c'}:
替换
while player_or_pc != 'u' or player_or_pc != 'c':
与
否则,player\u或\u pc
应该等于u
和c
,这是不可能的。player\u或\u pc!='u'或播放器或pc!='c'
-此条件始终为真。听起来你想要的是和
,而不是或
。
while player_or_pc != 'u' and player_or_pc != 'c':