名称错误:未定义名称3

2024-04-23 13:42:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一段代码(tic-tac-toe游戏),当我运行它时,python总是返回:

Traceback (most recent call last):
File "C:/Users/#####/Desktop/python stuff/tic tac toe.py", line 30, in  <module>
 if turn_2 == "cell1":
 NameError: name 'turn_2' is not defined

或者类似的。(你们都知道错误是什么样子的)。 这是产生问题的代码:

   turn2 = input("Enter what cell you want to play on: ")
   if turn_2 == "cell1":
        print("Put an X on cell 1.")
        cell1 = "x"
   elif turn_2 == "cell2":
        print("Put an X on cell 2")
        cell2 = "x"
   elif turn2 == "cell3":
        print("Put an X on cell 3")
        cell3 = "x"

我提前道歉,如果这看起来像垃圾,这是我第一次在网站上问问题。任何答案都非常感谢。 谢谢


Tags: 代码anifputoncellticturn
1条回答
网友
1楼 · 发布于 2024-04-23 13:42:13

问题是变量的命名和调用是不同的。您将变量命名为turn2,但在条件中使用它作为turn_2。将条件更改为if turn2 == "cell1":。(您的第二个条件也需要更改)。你知道吗

相关问题 更多 >