即使使用相同的方式编写,循环也不会停止

时间:2020-10-19 15:44:55

标签: python python-3.x python-2.7 while-loop python-requests

无法正常工作

def player_input():
marker = ''
while 'X' or 'O' not in marker:
      marker = input("Choose between 'X' or 'O': ")
      print ("Please choose between 'X' or 'O'")
     
if marker == 'X':
    return ('X', 'O')
else:
    return ('O', 'X')
        

工作代码

def player_input():
marker = ''
while not (marker == 'X' or marker == 'O'):
    marker = input("Choose between 'X' or 'O': ")
    print ("Please choose between 'X' or 'O'")
     
if marker == 'X':
    return ('X', 'O')
else:
    return ('O', 'X')

我都运行了,但是循环并没有首先停止。两者有什么区别?

1 个答案:

答案 0 :(得分:0)

您有缩进错误。 if语句不在循环中。试试这个代码。 while循环仅运行一次,因为您在if语句中使用过else

def player_input():
    marker = ''
    while not (marker == 'X' or marker == 'O'):
        marker = input("Please choose between 'X' or 'O' >")
        if marker == 'X':
            return ('X', 'O')
        else:
            return ('O', 'X')
print( player_input() )