Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python while循环或条件_Python_While Loop_Logical Operators - Fatal编程技术网

Python 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

在第二个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 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':