回溯错误 - python

时间:2017-04-05 21:58:33

标签: string input

我的代码工作在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

2 个答案:

答案 0 :(得分:1)

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

运行程序
python3 yourfile.py

避免错误。

答案 1 :(得分:1)

检查你的python版本&#34; python --version&#34;在命令行中。您可能安装了多个版本,并且可能正在使用2.x

或者尝试使用raw_input而不是输入。如果可行,您可以使用2.x

修改

  

如何删除旧版本的Python,因为100%这是问题

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

你是如何运行python程序的?如果您使用命令行&#34; python name.py&#34;,则可以替换&#34; python&#34;用&#34; python3&#34;用python 3解释器运行它。如果您使用的是IDE,他们可以选择python解释器,您可以在那里选择3.x.