【问题标题】:Why is `input` in Python 3 throwing NameError: name... is not defined [duplicate]为什么Python 3中的`input`会抛出NameError:name ...未定义[重复]
【发布时间】:2013-05-13 14:28:40
【问题描述】:

我有一个字符串变量test,在 Python 2.x 中可以正常工作。

test = raw_input("enter the test") 
print test

但在 Python 3.x 中,我会这样做:

test = input("enter the test") 
print test

输入字符串sdas,我收到一条错误消息

Traceback (most recent call last):
 File "/home/ananiev/PycharmProjects/PigLatin/main.py", line 5, in <module>
    test = input("enter the test")
 File "<string>", line 1, in <module> 
NameError: name 'sdas' is not defined

【问题讨论】:

  • 在 python3 中编写代码时,我在使用带有 python2 的终端时遇到了同样的问题
  • 您的 print test 语句不是 Python 3.x:它应该是 print(test)。事实上,python 正在尝试将 test 作为命令进行评估。

标签: python python-3.x string input eval


【解决方案1】:

如果我们抛开print的语法错误,那么在多种场景下使用input的方式是-

如果使用 python 2.x:

then for evaluated input use "input"
example: number = input("enter a number")

and for string use "raw_input"
example: name = raw_input("enter your name")

如果使用 python 3.x:

then for evaluated result use "eval" and "input"
example: number = eval(input("enter a number"))

for string use "input"
example: name = input("enter your name")

【讨论】:

    【解决方案2】:
    temperature = input("What's the current temperature in your city? (please use the format ??C or ???F) >>> ")
    
    ### warning... the result from input will <str> on Python 3.x only
    ### in the case of Python 2.x, the result from input is the variable type <int>
    ### for the <str> type as the result for Python 2.x it's neccessary to use the another: raw_input()
    
    temp_int = int(temperature[:-1])     # 25 <int> (as example)
    temp_str = temperature[-1:]          # "C" <str> (as example)
    
    if temp_str.lower() == 'c':
        print("Your temperature in Fahrenheit is: {}".format(  (9/5 * temp_int) + 32      )  )
    elif temp_str.lower() == 'f':
        print("Your temperature in Celsius is: {}".format(     ((5/9) * (temp_int - 32))  )  )
    

    【讨论】:

      【解决方案3】:

      在 Ubuntu 等操作系统中预装了 python。所以默认版本是 python 2.7 你可以通过在终端中输入以下命令来确认版本

      python -V
      

      如果你安装了它但没有设置默认版本,你会看到

      python 2.7
      

      在终端中。我会告诉你如何在 Ubuntu 中设置默认的 python 版本。

      一个简单安全的方法是使用别名。将其放入 ~/.bashrc~/.bash_aliases 文件中:

      alias python=python3
      

      在文件中添加上述内容后,运行以下命令:

      source ~/.bash_aliasessource ~/.bashrc

      现在使用python -V再次检查python版本

      如果 python 版本是 3.x.x 之一,那么错误就在你的语法中,比如使用带括号的 print。把它改成

      test = input("enter the test")
      print(test)
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的错误。在终端中,当我键入“python filename.py”时,使用这个命令,python2 正在运行 python3 代码,因为它是写 python3.py 的。当我在终端中键入“python3 filename.py”时,它运行正确。我希望这对你也有用。

        【讨论】:

        【解决方案5】:

        sdas 被作为变量读取。要输入一个字符串,你需要 " "

        【讨论】:

        • 你确定吗?据我了解,OP 希望 zu 传递一个已定义的变量,以便脚本评估该变量。
        • 你能详细说明你想说什么吗?如果提供代码sn-p会更好?
        【解决方案6】:

        您正在使用 Python 2 解释器运行 Python 3 代码。如果你不是,你的print 语句会在提示你输入之前抛出一个SyntaxError

        结果是您使用的是 Python 2 的 input,它尝试 eval 您的输入(大概是 sdas),发现它是无效的 Python,然后死掉。

        【讨论】:

        【解决方案7】:

        我会说你需要的代码是:

        test = input("enter the test")
        print(test)
        

        否则,由于语法错误,它根本不应该运行。 print 函数在 python 3 中需要括号。不过,我无法重现您的错误。你确定是那些行导致了这个错误吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-09
          • 1970-01-01
          • 2019-07-23
          • 2017-08-03
          • 2020-01-05
          • 2020-07-09
          • 2014-04-17
          • 1970-01-01
          相关资源
          最近更新 更多