Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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-读取文件并调整输出(不理解错误)_Python_Python 2.7 - Fatal编程技术网

Python-读取文件并调整输出(不理解错误)

Python-读取文件并调整输出(不理解错误),python,python-2.7,Python,Python 2.7,我的目标是编写一个将此文件作为输入的程序: Anne Adema____________6.5 5.5 4.5 Bea de Bruin__________6.7 7.2 7.7 Chris Cohen___________6.8 7.8 7.3 Dirk Dirksen__________1.0 5.0 7.7 并将其作为输出: Report for group 2b Anne Adema has an average grade of 5.5 Bea de Bruin has an

我的目标是编写一个将此文件作为输入的程序:

Anne Adema____________6.5 5.5 4.5 
Bea de Bruin__________6.7 7.2 7.7
Chris Cohen___________6.8 7.8 7.3 
Dirk Dirksen__________1.0 5.0 7.7 
并将其作为输出:

Report for group 2b
Anne Adema has an average grade of 5.5
Bea de Bruin has an average grade of 7.2 
Chris Cohen has an average grade of 7.3
Dirk Dirksen has an average grade of 4.6 End of report
这是我做的,但它不起作用,我不完全理解为什么。我认为把名字从分数上分开是错误的,但我不知道如何解决这个问题

import sys

def print_grade(input_grades):
    grades = input_grades.split()
    grade1 = float(grades[0])
    grade2 = float(grades[1])
    grade3 = float(grades[2])

    final_grade = (grade1 + grade2 + grade3) / 3
    print "%.2f." %final_grade

def print_names(student) :
    input_list = student.split("_+")
    full_name = input_list[0]
    final_grade = input_list[1]

    print "%s has an average grade of " % names
    print_grade(final_grade)

students = open('input.txt').readlines()

print "Report for group 2b"

for student in students:
    print_names(student)

print "End of report"
输出:

Traceback (most recent call last):
  File "...", line 25, in <module>
Report for group 2b
    print_names(student)
  File "...", line 15, in print_names
    final_grade = input_list[1]
IndexError: list index out of range
回溯(最近一次呼叫最后一次):
文件“…”,第25行,在
2b组报告
打印学生姓名(学生)
文件“…”,第15行,打印名称
最终成绩=输入列表[1]
索引器:列表索引超出范围

字符串的拆分方法不接受正则表达式。你应使用:

除此之外,变量名不存在,您应该使用全名:

为了不打印新行,您应在以下内容后使用逗号:


字符串的split方法不接受正则表达式。你应使用:

除此之外,变量名不存在,您应该使用全名:

为了不打印新行,您应在以下内容后使用逗号:


input\u list=student.split(“\u+”)

str.split()
无法识别正则表达式。这将被解析为字符串
。+
,它与您想要匹配的内容不匹配

这里没有索引==1,
final\u grade=input\u list[1]
,因为在上面试图拆分字符串的行中没有,所以它都在
input\u list[0]


可能需要导入re并使用
re.split(“\u+”,student)进行拆分
input\u list=student.split(“\u+”)

str.split()
无法识别正则表达式。这将被解析为字符串
。+
,它与您想要匹配的内容不匹配

这里没有索引==1,
final\u grade=input\u list[1]
,因为在上面试图拆分字符串的行中没有,所以它都在
input\u list[0]


可能想导入re并使用
re.split(“+”,学生)

进行拆分,为什么Chris Cohen的结果不应该单独一行?抱歉,这是个错误。我会编辑它为什么克里斯·科恩的结果不应该用它自己的一行呢?对不起,那是个错误。我会编辑它
re.split("_+", student)
print "%s has an average grade of " % full_name
print "%s has an average grade of " % full_name,