【问题标题】:Basic while loop issue [closed]基本的while循环问题[关闭]
【发布时间】:2017-10-19 18:40:34
【问题描述】:

我正在使用下面提供的代码创建一个简单的井字游戏。当我运行此代码时,我得到一个无效的语法错误,True 被突出显示。 While True 循环不应该无限期地运行吗?

# Tic-Tac-Toe 
board = [1, 2, 3,
         4, 5, 6,
         7, 8, 9]

def printBoard():
      print (board[0], '|', board[1], '|', board[2], '|' )
      print('- - - - - - ')
      print (board[3], '|', board[4], '|', board[5], '|' )
      print('- - - - - - - -')
      print (board[6], '|', board[7], '|', board[8], '|' )
      print('- - - - - - ')

While True:

move = input('Enter a number from the board?')
move = int(move)

if board[input] != 'x' or board[input] != '0':
    board[input] = 'x'
else:
    print('this spot is taken')

printBoard()

【问题讨论】:

  • 缩进在 Python 中很重要。

标签: python loops while-loop


【解决方案1】:

While 必须全部小写:“while”。那是sintax错误

【讨论】:

    【解决方案2】:

    您需要删除While True:move = input 之间的空行

    Python 关心每一个缩进。另外,请确保while 是小写的。

    【讨论】:

      【解决方案3】:

      您的While 不应大写。应该是while。还要注意你的缩进,在 while 块内看不到任何东西。

      【讨论】:

      • 感谢您对否决票的反馈。
      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2020-06-03
      相关资源
      最近更新 更多