Python2中input出现的name “xxx” is not defined问题原因及解决办法
来源:互联网 发布:淘宝网的案例分析 编辑:程序博客网 时间:2024/05/17 08:16
# coding=UTF-8'''Created on 2017年10月22日@author: Dyna'''str_1 = input("Enter a string:")str_2 = input("Enter another string:")print ("str_1 is:"+str_1+" str_2 is:"+str_2)print "str_1 is {} ,str_2 is {}".format(str_1, str_2)以上为用来测试Python中的输入函数input:但是出现了以下情况:
Enter a string:helloTraceback (most recent call last): File "/Users/Dyna/Documents/workspace/TeachingPython/Test_IO_Format.py", line 7, in <module> str_1 = input("Enter a string:") File "/Users/Dyna/Downloads/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev_4.5.5.201603221110/pysrc/pydev_sitecustomize/sitecustomize.py", line 141, in input return eval(raw_input(prompt)) File "<string>", line 1, in <module>NameError: name 'hello' is not defined
我在输入hello时,进行报错,
NameError: name 'hello' is not defined。
上Python官网上查询了一下文档,原因定位如下:
Python 2.X中对于input函数来说,它所希望读取到的是一个合法的Python表达式,即你在输入字符串的时候必须要用""将其扩起来,我的Python版本为2.7,因此出现这个问题,而在Python 3中,input默认接受的是str类型。
解决办法:1、在控制台进行输入参数时,将其变为一个合法的Python表达式,用""将其扩起来
2、使用raw_input,因为raw_input将所有的输入看作字符串,并且返回一个字符串类型。
1、
Enter a string:"hello"Enter another string:"Python"str_1 is:hello str_2 is:Pythonstr_1 is hello ,str_2 is Python2、
# coding=UTF-8'''Created on 2017年10月22日@author: Dyna'''str_1 = raw_input("Enter a string:")str_2 = raw_input("Enter another string:")print ("str_1 is:"+str_1+" str_2 is:"+str_2)print "str_1 is {} ,str_2 is {}".format(str_1, str_2)
阅读全文
0 0
- Python2中input出现的name “xxx” is not defined问题原因及解决办法
- python中input输出“Name 'XXX' is not defined问题
- 使用python经常出现NameError: name 'xxx' is not defined
- jsp引入bootstrap、bootstrap-Validator后出现xxx is not a function的问题及解决办法!
- python name 'file' is not defined的解决办法
- python name 'file' is not defined的解决办法
- python import 产生的问题(name 'xml' is not defined)
- python2.7以下出 NameError: global name 'FileNotFoundError' is not defined的解决方案
- Python编译时提示“NameError: name 'Popen' is not defined”及”NameError: name 'PIPE' is not defined“错误的解决
- paip.python NameError name 'xxx' is not defined\
- python提示NameError: name 'xxx' is not defined
- 出现类似'CListView' : is not a class or namespace name的原因及解决方法
- 编译时出现fatal error C1853: "debug/xxx.pch" is not a precomplied header file with......错误的原因及解决办法(转)
- python raw_input or input With name 'abc' is not defined
- Hibernate 中出现 xxx表 is not mapped xxx的问题
- input的onclick事件调用报错Uncaught ReferenceError: XXX is not defined
- wsdl2java "XXX is referenced but not defined."解决办法
- jQ中遇到的小问题-----Jquery调用出现ReferenceError: $ is not defined
- 详解从零开始配置汇编开发环境(DosBox,MasmPlus)
- 跨页面传值的几种简单方式
- Redis 数据结构与实现
- VS2017 Visual Assistant(一键搞定,亲测可用)
- 第八周 项目4-稀疏矩阵的三元组表示的实现及应用
- Python2中input出现的name “xxx” is not defined问题原因及解决办法
- struts2实现文件上传下载
- 论Docker容器中的Nginx配置文件如何读取系统/容器的环境变量
- 洛谷 2587 泡泡堂(贪心)
- go语言实现简易ftp客户端
- Jvm内存划分
- 记录自己对于c/c++的DSA学习历程 1-3
- Struct 与 Class 的初始化赋初值(struct篇)
- WPF控件的一些特殊应用