我的这段代码为什么会报列表越界错误

正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学,您好,看同学的报错情况,

http://img1.sycdn.imooc.com//climg/5d00c7f90001b36706380375.jpg

我猜测同学的txt文档最后可能存在空行之后在空格的情况,同学可删除之前的考试成绩.txt文档,重新创建个考试成绩.txt文档,复制粘贴下列数据到考试成绩.txt,粘贴完成之后,直接关闭该文档,注意:粘贴时看光标是否自动定位在文档开头,粘贴完成直接关闭文档。

620,王伟,2-1,96,89,75
621,赵芳芳,2-1,71,62,80
622,徐丽丽,2-2,78,95,69
623,张倩,2-2,96,88,89
624,胡倩倩,2-2,98,99,100
625,赵晓婷,2-3,91,87,89
626,李伟,2-3,53,68,67
627,王秀英,2-4,86,87,87
628,李静,2-4,65,68,74
629,郭楠楠,2-4,78,81,94
630,邓逸凡,2-4,70,66,61

如果我的回答解决了您的疑惑,望采纳!!^_^ 祝学习愉快~~~~~

提问者 不爱吃饺子 2019-06-12 15:59:36
from redis_db import pool
import redis

con = redis.Redis(
    connection_pool=pool
)

try:
    file = open(file="考试成绩.txt", mode="r", encoding="utf-8")
    data = file.read().splitlines()
    for index in data:
        temp = index.split(",")
        sid = temp[0]
        name = temp[1]
        class_no = temp[2]
        score_1 = int(temp[3])
        score_2 = int(temp[4])
        score_3 = int(temp[5])
        if score_1 >= 85 and score_2 >= 85 and score_3 >= 85:
            con.hmset(sid, {"name": name, "class_no": class_no,
                              "语文": score_1, "数学": score_2, "外语": score_3})
    print("执行成功!")
except Exception as e:
    print(e)
finally:
    if "file" in dir():
        file.close()
    del con

-------------------------------------------------------------------------------

620,王伟,2-1,96,89,75
621,赵芳芳,2-1,71,62,80
622,徐丽丽,2-2,78,95,69
623,张倩,2-2,96,88,89
624,胡倩倩,2-2,98,99,100
625,赵晓婷,2-3,91,87,89
626,李伟,2-3,53,68,67
627,王秀英,2-4,86,87,87
628,李静,2-4,65,68,74
629,郭楠楠,2-4,78,81,94
630,邓逸凡,2-4,70,66,61

  • 同学,您好,我们测试了下您的代码,没有发现问题,同学可print(temp),将捕获异常的try:,except Exception as e:,finally:去掉,看看控制台会报什么错误。将报错信息截图,贴图贴出来。
    2019-06-12 16:51:14
  • 提问者 不爱吃饺子 回复 慕猿梦 #2
    from redis_db import pool import redis con = redis.Redis( connection_pool=pool ) file = open(file="考试成绩.txt", mode="r", encoding="utf-8") data = file.read().splitlines() for index in data: temp = index.split(",") print(temp) sid = temp[0] name = temp[1] class_no = temp[2] score_1 = int(temp[3]) score_2 = int(temp[4]) score_3 = int(temp[5]) if score_1 >= 85 and score_2 >= 85 and score_3 >= 85: con.hmset(sid, {"name": name, "class_no": class_no, "语文": score_1, "数学": score_2, "外语": score_3}) print("执行成功!") ------------------------------------------------------------------------------------- ['620', '王伟', '2-1', '96', '89', '75'] ['621', '赵芳芳', '2-1', '71', '62', '80'] ['622', '徐丽丽', '2-2', '78', '95', '69'] ['623', '张倩', '2-2', '96', '88', '89'] ['624', '胡倩倩', '2-2', '98', '99', '100'] Traceback (most recent call last): ['625', '赵晓婷', '2-3', '91', '87', '89'] File "D:/py_learn/demo_2/example_7.py", line 14, in <module> ['626', '李伟', '2-3', '53', '68', '67'] name = temp[1] ['627', '王秀英', '2-4', '86', '87', '87'] IndexError: list index out of range ['628', '李静', '2-4', '65', '68', '74'] ['629', '郭楠楠', '2-4', '78', '81', '94'] ['630', '邓逸凡', '2-4', '70', '66', '61'] [''] Process finished with exit code 1
    2019-06-12 17:06:06
慕猿梦 2019-06-12 15:57:14

同学,您好,同学可将代码复制粘贴出来,老师帮您调试一下。

如果我的回答解决了您的疑惑,望采纳!!^_^ 祝学习愉快~~~~~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师