用户登录
用户注册

分享至

为什么python编译fourth = raw.input(‘Year: ‘)[3]时会报错

  • 作者: 留_东临天下
  • 来源: 51数据库
  • 2021-08-26

最近在学习Python,我用的是教材是:Python基础教程(第二版)作者是[挪]Magnus Lie Hetland 由司维 增军崴 谭颖华翻译。我现在使用的Python版本为:Python 3.8.5。
在学习第二章列表的时候发现用python编译
fourth = raw.input('Year: ')[3]时报错了,编译器提示Traceback (most recent call last): File “<pyshell#14>”, line 1, in fourth = raw.input('Year: ')[3] NameError: name ‘raw’ is not defined
在网上没搜到答案,倒是看到有的人在使用Python2.7时就出现这个问题了。我尝试把下划线改为点 . 结果不起作用,接着我直接把raw和下划线去掉时,发现可以运行了,以下是演示过程:

>>> fourth = raw_input('Year: ')[3]
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    fourth = raw_input('Year: ')[3]
NameError: name 'raw_input' is not defined
>>> fourth = raw.input('Year: ')[3]
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    fourth = raw.input('Year: ')[3]
NameError: name 'raw' is not defined
>>> fourth = input('Year: ')[3]
Year: 2006
>>> fourth
'6'

可能是由于作者的书籍版本比较老,他原来的算法在新版本软件上已经不能运行了。

软件
前端设计
程序设计
Java相关