【问题标题】:File "<string>", line 1, in <module> NameError: name ' ' is not defined in ATOM [duplicate]<module> 中的文件“<string>”,第 1 行 NameError:名称''未在 ATOM 中定义 [重复]
【发布时间】:2019-03-05 04:01:50
【问题描述】:

这很奇怪,因为我的代码在在线 python 解释器中运行良好,但是当我使用 Atom 在 linux mint 中运行它时,当我输入一个单词时出现以下错误消息:

File "<string>", line 1, in <module>
NameError: name 'lol' is not defined

这是我的代码

# -*- coding: utf-8 -*-

word = str(input(" Enter a word : "))
reverse = word[::-1]
if reverse == word:
  print("it is a palindrome, félicitation : ")
else:
  print(" it is not a palindrome : ")

【问题讨论】:

    标签: python python-3.x atom-editor


    【解决方案1】:

    尝试使用raw_input 而不是input。听起来您可能在在线解释器中运行 Python 3 中的代码,其中 input 的行为类似于 Python 2 的 raw_input,并在本地使用 Python 2。

    在 python 2 中,input 会导致您的代码为您的输入寻找定义,而不是将其作为字符串。

    【讨论】:

    • 非常感谢!它工作正常,但真正的问题是我发现我在 atom 中使用的是 python 2 而不是 python 3。我使用 help() 来查找我使用的版本。我不是专业人士。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2020-06-17
    • 2016-07-06
    • 2015-10-22
    • 2016-05-12
    相关资源
    最近更新 更多