Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 - Fatal编程技术网

Python浮点/求和函数

Python浮点/求和函数,python,Python,a。阅读文件并报告每个学生的成绩。分数是每个问题的分数之和。这些列以制表符分隔。您需要使用float()函数将字符串转换为float。还可以使用sum()函数,该函数接受一个列表作为输入,并返回其元素的总和 原始文件: %%file midterm-grades.txt 1.1 1.2 1.3 1.4 1.5 2.1 2.2 2.3 3.1 3.2 3.3 3.4 3.5 3.6 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 5.1 5.2 5.3 5.4a 5.4b

a。阅读文件并报告每个学生的成绩。分数是每个问题的分数之和。这些列以制表符分隔。您需要使用float()函数将字符串转换为float。还可以使用sum()函数,该函数接受一个列表作为输入,并返回其元素的总和

原始文件:

%%file midterm-grades.txt
1.1 1.2 1.3 1.4 1.5 2.1 2.2 2.3 3.1 3.2 3.3 3.4 3.5 3.6 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 5.1 5.2 5.3 5.4a    5.4b    5.5 5.6
===========================================================================================================================
1   1   1   1   1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
===========================================================================================================================
0   0.5 0.5 1   1   3   2   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   1.5 2   0   1.5 1.5 1.5
0   0.5 1   1   1   3   3   4.5 1.5 1.5 1.5 1.5 0.5 1.5 0   0   0   0   0   0   0   0   2   2   1.5 1.5 2   2   2
0   0.7 0.5 1   1   3   0   0   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   2   2   1   2   2
1   0.5 0.8 1   1   3   0   0   1.5 1.5 1.3 1.5 1.5 0.7 1.5 1.5 1.5 1.5 0   1.5 1.5 1.5 2   1.5 2   0   2   2   2
1   1   1   1   1   1.5 2   2   1.5 1.5 1   1.5 1.5 0.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   1
1   0.7 0.5 0   0   2.5 0   0   1.5 1.5 1.5 1.5 0   0.5 1.5 1.5 1.5 0.5 0.5 0   0.5 0.5 2   2   2   0   0   0   0
1   1   1   1   1   3   0   0   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 2   2   2   0   2   1.5 0
1   0.7 0.8 1   1   3   3   3   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   1.5 1.5 0   0   0
1   1   1   1   1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   1.5 1.5 0   1   0
1   1   1   1   1   2.5 3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0   1.5 1.5 1.5 2   2   2   2   2   2   2
1   1   1   1   1   2   3   4.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 0.5 1.5 2   2   1.5 2   2   2   2
1   0.5 1   1   1   3   2.5 4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   2
1   0.7 0.8 1   1   1.5 3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   1.5 1   2   2
1   1   1   1   1   3   3   3   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 0   1   0   0
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
1   0.7 0.5 1   1   1.5 1   0.5 1.5 1.5 1.5 1.5 1.5 0   1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 2   2   1.5 2   0   0   0
1   0.7 0.8 1   1   0   1   0.5 0   1.5 1   1.5 0   0.5 1.5 1.5 1.5 1.5 1.5 0   0.5 1.5 2   2   1.5 0   0   0   0
1   0.7 0.8 1   1   1.5 0   0.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   2   0   2   2   2
1   0.7 0.8 0.5 1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   0   2   2   1.5
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   2   2   2   2   2
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   2
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   1   1   2
1   1   1   1   1   3   2   3   1.5 1.5 1   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   2   2   1.5 0   2   0   2
1   1   1   1   1   3   3   5   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.3 1.5 2   2   2   2   2   1   2
1   1   1   0.5 0.5 2.5 0   0   1.5 1.5 1.5 1.3 0   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 0   1   2   0
1   1   1   1   1   3   0   0   1.5 1.5 1   1.5 0   1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 2   2   1.5 0   2   2   0
1   0.5 0.8 1   1   3   0   0   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1   1.5 2   2   2   0   2   2   2
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1   1.5 2   2   2   0   1   2   2
1   0.7 1   1   1   3   2   4   1.5 1.5 1   1.5 1.5 1   1.5 1.5 1.5 1.5 1   1.5 0.5 1.5 2   2   2   0   2   2   2
1   0.5 1   1   1   3   2   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1   2   2   2
1   0.7 1   1   1   2.5 3   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 0.5 1.5 2   2   1.5 0   2   2   1.5
1   0.7 0.8 1   1   3   2   2   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1.5 1.5 2   2   1   0   2   2   2
1   1   1   1   1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   2
1   0.5 0.3 1   1   1.5 3   3.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 0   1   1   1
1   0.8 1   0   0   0   0   0   1.5 1.5 1   1.5 1.5 0   1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 2   2   1.5 1.5 2   2   1.5
1   0.5 0.8 1   1   3   1   1.5 1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
1   0.3 0.8 1   1   1.5 3   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1   1.5 2   2   1.5 1.5 2   2   2
1   1   1   1   1   3   2   2   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
这是我的代码:

# Read file
midterm = open("midterm-grades.txt", "r")
# Report grade for each student
midterm_content = midterm.read()
indiv = midterm_content.split("\n")
# Get rid of Headers
students = indiv[4:]
for i in students : 
    withouttabs = i.split("\t")
    print "Student grade is ", sum(float(withouttabs))

无法计算出如何浮动等级以便执行求和函数,或者在这一点上几乎完全丢失了。

如前所述,无法在字符串列表上浮动()。只需在单个字符串列表上迭代,然后一次浮起一个字符串,如下所示:

midterm = open("midterm-grades.txt", "r")
# Report grade for each student
midterm_content = midterm.read()
indiv = midterm_content.split("\n")
# Get rid of Headers
students = indiv[4:]
for i in students : 
    withouttabs = i.split("\t")
    total = 0   ## initialize total for each student
    for j in withouttabs:   ## now, iterate over each list of strings
        total = total + float(j)
    print "Student grade is ", total   ## finally, print the total

假设您得到的是
TypeError:float()参数必须是字符串或数字
,在这种情况下,您应该发布的是
float([])
-您需要分别转换列表中的每个数字。您能提供一些文件行吗?嗨,Jon,谢谢您回复我!在原始数据集中,有很多数字。有没有更快的方法来做到这一点?@Kasramvd我们没有学生的名字;我们只得到了一堆行,每一行代表一个“学生”。我得到的错误是
TypeError:“float”对象不可调用
谢谢,我仍然了解这个问题。这更好吗?肯定有进步,谢谢你们两位!我仍在努力理解所有Python术语,因此,对于我造成的任何困惑,我深表歉意。