python中input()和raw_input的区别

在使用的Python2.7.15的版本,今日初学Python中遇到了一个错误,在使用input()函数中会出现错误,错误代码如下:

name = input("Input your name:")
age = input ("Input your age:")
print("================")
print("name: %s"%name)
print("age: %s"%age)
print('====================')

在输入字符串姓名以后,报错

Input your name:shanghai

Traceback (most recent call last):
  File "E:/python学习/yanshi.py", line 1, in <module>
    name = input("Input your name:")
  File "<string>", line 1, in <module>
'NameError: name 'shanghai' is not defined

发现NameError,shanghai这个名称没有定义

查阅资料后发现,Python2中和Python3中input()函数的不同

区别

1>python3里面已经把raw_input()给去掉了
事实上是这样的:在 Python 3 内,将 raw_input() 重命名为 input(),这样一来,无须导入也能从标准输入获得数据了。如果您需要保留版本 2.x 的 input() 功能,可以使用 eval(input()),效果基本相同。
2>Python 版本 2.x 中,raw_input() 会从标准输入(sys.stdin)读取一个输入并返回一个字符串,且尾部的换行符从末尾移除
3>raw_input()的输入都是按照字符串处理,而input()必须按照Python的规则进行
例如:

  • age = raw_input(“Input your age :”)
  • age = input (“Input your age”)
    当你输入的都是18时,raw_input()当做字符串‘18’处理,而input()当做整型18处理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值