Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在if语句的列表中使用索引_Python - Fatal编程技术网

Python 在if语句的列表中使用索引

Python 在if语句的列表中使用索引,python,Python,我正在为类编写代码,但遇到了问题。我的故障排除对我没有帮助。我遇到的第一个问题是,当将读取的文件拆分为列表中索引的[name,score]格式时,它会读取额外的行,但也会读取正确的行。然后在写入输出文件时,它只读取列表中的最后一个索引,但对于列表中的该索引,这是正确的输出。我一直在读我的书,但找不到如何使用列表及其索引来遍历第二条if语句。这是任务: 编写一个程序,从名为“bowlingscores.txt”的外部文件中读取未知数量的保龄球手及其保龄球得分(可能值为1到300)。该文件类似于以下

我正在为类编写代码,但遇到了问题。我的故障排除对我没有帮助。我遇到的第一个问题是,当将读取的文件拆分为列表中索引的[name,score]格式时,它会读取额外的行,但也会读取正确的行。然后在写入输出文件时,它只读取列表中的最后一个索引,但对于列表中的该索引,这是正确的输出。我一直在读我的书,但找不到如何使用列表及其索引来遍历第二条if语句。这是任务: 编写一个程序,从名为“bowlingscores.txt”的外部文件中读取未知数量的保龄球手及其保龄球得分(可能值为1到300)。该文件类似于以下内容:David 102 Hector 300 Mary 195 Jane 160 Sam 210将投球手的姓名输出到名为“bowlingaverages.txt”的外部数据文件中。在每个投球手的名字旁边,根据他们的分数打印一条消息:对于满分(等于300),对于高于平均分的分数输出“完美”,对于低于平均分的分数输出“高于平均值”,输出“低于平均值”您的程序必须至少包含一个函数(例如,计算平均值,确定要打印的适当消息等)


这是我得到的输出。粗体打印是我需要输出的输出。不是所有额外的。每个名称都应该像最后一行一样显示。
[('David',0),('David',102),('Hector',102),('Hector',300),('Mary',300),('Mary',195),('Jane',195),('Jane',160),('Sam 160),('Sam 210)]
Sam,210,高于平均值

我不确定您的循环结构是否正确,以及读取文件时是否正确读取字符串

我不想太多地干扰你的原始代码。所以我在这里和那里做了一些更改。你可以使你的代码更加紧凑

infile = open("bowlingscores.txt", "r")
name=0
score=0
averagescore=0
count=0
bowlersinfo=[]
for line in infile:
   bowler,score= line.split()
   score = int(score)
   averagescore += score
   bowlersinfo.append([bowler,score])
print (bowlersinfo)


infile.close()

averagescore /= float(len(bowlersinfo))

outfile = open("bowlingaverages.txt", "w")

for name,score in bowlersinfo:
 if score==300:
     outcome= "Perfect"
 elif score > averagescore:
     outcome= "Above Average"
 elif score== averagescore:
     outcome= "Average"
 else:
     outcome= "Below Average"
 print ("%s, %0.0f, %s" % (name, float(score), outcome))
 outfile.write(name+": " ) 
 outfile.write(outcome+"\n")
outfile.close()

用粗体字冷静下来,不要对我们大喊大叫。我们不需要作业,我们需要问题。在这种情况下,字典会更有用。也许你应该研究一下。粗体字很抱歉。我的老师告诉我们她想要的是列表格式,而不是字典。
infile = open("bowlingscores.txt", "r")
name=0
score=0
averagescore=0
count=0
bowlersinfo=[]
for line in infile:
   bowler,score= line.split()
   score = int(score)
   averagescore += score
   bowlersinfo.append([bowler,score])
print (bowlersinfo)


infile.close()

averagescore /= float(len(bowlersinfo))

outfile = open("bowlingaverages.txt", "w")

for name,score in bowlersinfo:
 if score==300:
     outcome= "Perfect"
 elif score > averagescore:
     outcome= "Above Average"
 elif score== averagescore:
     outcome= "Average"
 else:
     outcome= "Below Average"
 print ("%s, %0.0f, %s" % (name, float(score), outcome))
 outfile.write(name+": " ) 
 outfile.write(outcome+"\n")
outfile.close()