所以我有一个
名字(空格)姓氏(制表符)等级
示例
万达理发师 96个
我很难将此作为列表读入,然后编辑号码
我现在的代码是
def TopStudents(n):
original = open(n)
contents = original.readlines()
x = contents.split('/t')
for y in x[::2]:
y - 100
if y > 0: (????)
这就是我困惑的地方。我只是想知道得分超过100%的学生的名字和姓氏。我想为符合这个条件的学生建立一个新的名单,但我不确定如何写出相应的名字和姓氏。我知道我需要在列表中的每个其他位置上迈出一大步,因为名字和姓氏总是odd。提前感谢您的帮助
您的代码有几处错误:
-打开的文件必须关闭(#1)
-必须使用函数调用来调用它(#2)
-使用的拆分使用正斜杠(/)而不是反斜杠()(#3)
-如果您希望访问所有成员(#4),那么您决定在for循环中循环的方式不是最佳方式
-for循环以
:
(#5)-您必须将计算结果存储在某个位置(#6)
也就是说,固定版本可以是:
注意:为了帮助您理解代码,我重点介绍了可读性和一些注释
我总是喜欢使用'with open()',因为它会自动关闭文件。为了简单起见,我使用了带有逗号分隔的txt,但是您可以将逗号替换为\t
这将打开并将所有行作为列表加载到内容中,从而删除空行和换行符。然后它分成一个列表
然后遍历x中的每个列表,查找第二个值(y[1]),这是您的成绩。如果int()大于100,则打印y的每一段
相关问题 更多 >
编程相关推荐