【问题标题】:traceback error - python回溯错误 - python
【发布时间】:2017-08-31 17:26:43
【问题描述】:

我的代码在 Win 机器、python 3.6 上运行,然后我在 mac - 终端上尝试代码并显示错误。还安装了最新版本的python 我的代码:

print ("==================================")
print ("    Print all String Character    ")
print ("==================================")

word_secund = input("Please add your word: ")
word_one = str(word_secund)

def list(text):
    word = 0
    for word in text:
        print(word)
print (list(word_one))

错误信息:

Please add your word: Slavo
Traceback (most recent call last):
  File "strings.py", line 5, in <module>
    word_secund = input("Please add your word: ")
  File "<string>", line 1, in <module>
NameError: name 'Slavo' is not defined

【问题讨论】:

  • 尝试raw_input 而不是input 我怀疑虽然你已经在你的Mac上安装了最新版本的python,但你的机器上安装了两个版本的python,它是python 2+默认。
  • 我认为你是对的,我有冲突

标签: string input


【解决方案1】:

此错误发生在 python2 中。请注意,在 Mac 和 Linux 上,默认的 python 是 python2。尝试使用命令运行你的程序

python3 yourfile.py

为了避免错误。

【讨论】:

  • 谢谢各位,我会的
  • 不客气。请不要忘记批准其中一个答案以关闭问题。
【解决方案2】:

在命令行中检查您的 python 版本“python --version”。您可能安装了超过 1 个版本,并且您可能正在使用 2.x

或者尝试使用 raw_input 代替 input。如果可行,您使用的是 2.x

编辑:

我怎样才能删除旧版本的 Python,因为 100 % 这是问题

您可能不想删除系统默认使用的 Python 2.7。检查这个答案:Remove all previous versions of python

你是如何运行你的python程序的?如果您使用带有命令“python name.py”的命令行,则只需将“python”替换为“python3”即可使用 python 3 解释器运行它。如果您使用的是 IDE,他们有一个选择 python 解释器的选项,您可以在那里选择 3.x。

【讨论】:

  • 如何删除旧版本的 Python,因为 100 % 这是问题所在。
猜你喜欢
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 2022-01-01
  • 2021-07-14
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 2011-01-14
相关资源
最近更新 更多