【问题标题】:Index out of range for the list?索引超出列表范围?
【发布时间】:2020-01-07 18:17:25
【问题描述】:

'''

marksheet=[]
scorelist=[]
for i in range(0,int(input())):
    name=input()
    score=float(input())
    marksheet.append([name,score])
    scorelist.append(score)
second_marks=sorted(list(dict.fromkeys(score_list)))[1]# @here code is reflecing error that it list index is out of range.

对于名称,已排序的标记(mark_sheet): 如果标记 == second_lowest_mark: 打印(名称)

'''

【问题讨论】:

  • 这段代码没有多大意义——请修正缩进并描述你给input的内容(更好的是,只是对失败的数据进行硬编码)。 score_list 未定义。请提供minimal reproducible example。什么是预期的输出?感谢您的澄清。
  • marksheet 和 scorelist 是空列表,用户可以输入任何编号。并基于该编号,用户将输入该编号的次数名称和分数。

标签: python python-3.x list nested-lists


【解决方案1】:

这里的错误是 NameError: name 'score_list' is not defined。换一下看看。

marksheet=[]
scorelist=[]
for i in range(0,int(input())):
    name=input()
    score=float(input())
    marksheet.append([name,score])
    scorelist.append(score)
second_marks=sorted(list(dict.fromkeys(scorelist)))[1]

我认为这是您遇到的问题。除非你的预期输出是什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 2011-06-14
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多