Python 名称错误 : name 'hello' is not defined

标签 python nameerror

<分区>

当我尝试在 Python 3.0 中使用任何可能的输入字符串执行以下代码时,出现 NameError:

def onePerLine(str):
    for i in str:
        print(i)

word=input("Enter a phrase or word: ")
onePerLine(word)

错误如下:

Enter a phrase or word: hello

Traceback (most recent call last):File"C:\Users\R\Documents\Python30\func2.py",line 5, in <module> word=input("Enter a phrase or word: ")

File "<string>", line 1, in <module>

NameError: name 'hello' is not defined 

我该如何解决这个问题并让我的代码运行? PS:我是 python 和一般编程的新手。任何援助将不胜感激。

最佳答案

您使用的是 Python 2,所以您需要使用 raw_input

>>> x = input('')
hello

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    x = input('')
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

使用原始输入

>>> x = raw_input('')
hello
>>> x
'hello'

关于Python 名称错误 : name 'hello' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722883/

相关文章:

Python 名称错误 : name 'file_name' is not defined

Python 函数相当于 * 来扩展参数?

python - NameError:全局名称 'NAME' 未定义

python - 递归函数未在其内部定义

python - 从外部模块调用类会导致 NameError,在 IDLE 中它工作正常

python - 无法在 python 中停止 nameerror

python - 带有 memcached : how to make it work? 错误的 Pyramid - MissingCacheParameter:需要 url

python - python3源代码的类图查看器应用程序

Python递归文件夹读取

python - numpy 和 statsmodels 在计算相关性时给出不同的值,如何解释?