欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python2.x中raw_input()和python3.x中的input()的区别讲解

程序员文章站 2024-01-13 10:43:22
跟着书敲代码的时候突然发现运行raw_input()函数一直报错 Traceback (most recent call last): File "C:\...

跟着书敲代码的时候突然发现运行raw_input()函数一直报错

	Traceback (most recent call last):
  	File "C:\Users\Desktop\python\name.py", line 1, in 
   	 name=raw_input("what's your name?"+"\n")
	NameError: name 'raw_input' is not defined

不知为何(我的Python版本是3.6.3),后来查看了文档才知道原来Python2.X版本中存在raw_input()和input()两个函数,但是

Python3.X版本中认为raw_input()是冗余函数就将其作为垃圾扔掉了,因此在运行Python3.X版本时需要将所有的raw_input()替换为input()才能执行。

值得注意的是在Python3.X版本中,input()函数接收所有输入,并默认将所有的输入都看作字符串来处理,返回字符串类型。

以下是修改之后的代码:

	name=input("what's your name?"+"\n")
	print("hello "+name)

	== RESTART: C:\Users\Desktop\python\name.py ==
	what's your name?
	helloworld
	hello helloworld