Python3 Error: list index out of range

萌德咯 萌德咯 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


f = open ('userinfo.txt')#flie_ad
lines = f.readlines()
num_f = 0
num_m = 0
for i in lines:
name = i.split(',')[0]
birthday = i.split(',')[1]
print(name,birthday)
if i.split(',')[2] == 'male':
num_m = num_m+1
else:
num_f = num_f +1
print ("There is %d male emp" %num_m)
print ("There is %d emp in this office" %(num_m+num_f))




userinfo.txt

fox,1995,male,
hanmeimei,1990,female,
liming,1989,male,
asdf,1995,female,



执行结果为

fox 1995
hanmeimei 1990
liming 1989
asdf 1995
Traceback (most recent call last):
Flie "test.py", line 14, in <module>
birthday = i.split(',')[1]
IndexError: list index out of range


这个报错无法理解 15行print已经成功输出了 反过头说14行超限了

python版本是3.5.0
系统是Red Hat Enterprise Linux 6
用vim编译
...全文
评论 3
收藏 已收藏
分享
返回顶部
萌德咯
脚本语言 37,738 成员 34,208 内容