【问题标题】:I need help finding the arithmetic mean in my Python program我需要帮助在我的 Python 程序中找到算术平均值
【发布时间】:2011-11-20 08:23:14
【问题描述】:

我已经编写了几乎所有的程序,除了我被困在这个特定的部分。我需要写出一个平均值来计算所有学生的最终成绩作为课程的统计数据。学生姓名和最终成绩已附加到外部文件中(请注意,可以附加更多学生和成绩)。

这是我目前所拥有的,任何意见都非常感谢。

fname = input("What is the file name?")
file1 = open(fname,'r')
sum = 0.0
count = 0
for line in file1:
    sum = sum + eval(line)                                     
    count = count + 1

print("\nThe average of the numbers is", sum/count)

在第 6 行 (sum = sum + eval(line)) 我不断收到

syntax error:  unexpected EOF while parsing (<string>, line 1)

我对 Python 的了解还不够,无法知道这意味着什么。有人可以用代码告诉我吗?供参考的外部文件格式如下:

tom jones,100,
bill smith,89,

等等。

【问题讨论】:

  • 评估这条线毫无意义。从行中解析整数。 (或者,如果它们实际上是用逗号而不是整数分割的,那么用逗号分割并迭代它并解析出整数。)
  • eval?啊! (我引用了一个在 JavaScript 社区中广泛使用的表达方式:“eval is evil”。)
  • 您可能想要使用csv 模块。
  • 现在看到格式是什么,你应该做什么,Emily Ball,循环遍历每一行(正是你在做什么),然后用逗号分割。第一个元素(0 索引)将是您的数字,您可以将其添加到 sum。 docs.python.org/library/stdtypes.html#str.split 编辑:克里斯是对的。使用 csv 模块会更容易,因为它会正确解析内部有逗号等的引号。

标签: python-3.x


【解决方案1】:

你得到的错误是:

Traceback (most recent call last):
  File "stud.py", line 6, in <module>
    sum = sum + eval(line)
  File "<string>", line 1
    tom jones,100,
            ^
SyntaxError: invalid syntax

这是因为您试图将“tom jones,100,”作为 Python 表达式进行计算。这不是一个有效的 Python 表达式。更不用说在任意字符串上调用eval() 是一个非常糟糕的主意。

您需要split 行,使用',' 作为分隔符。然后您需要获取第二个字段 (100),并将其转换为 int。您可以将此int 添加到您的sum(或total)并继续。

N.B:sum() 是 Python 中的内置函数,您使用同名变量隐藏它。我建议使用其他作品,例如total

祝你好运!

【讨论】:

  • 我只是不看代码就无法理解......对不起,我对此很陌生
  • 我不能为你做作业。我只能解释为什么您的代码会以这种方式运行,并为您指明正确的方向。如果您按照我的答案中的链接进行操作,您应该有足够的信息来完成您的任务。
  • int(line.split(',')[1]) 就足够了。如果您看到list index out of range,那么您的输入与您提供的示例不匹配。
【解决方案2】:

首先,您应该尝试进入Python interactive mode。它使使用少量代码变得更容易,因为您可以立即看到发生了什么。

您可以使用str.split 将字符串拆分为值,而不是使用 eval。启动交互式解释器,并逐行运行以下代码:

a = '1,2,3'
b = a.split(',')
print b
print b[0]
print b[0] + b[1]
print float(b[0]) + float(b[1])

b[0] + b[1] 打印为 '12' 的原因是因为它们仍然是字符串。你需要告诉 python 在它们像你期望的那样工作之前将它们作为数字处理(使用 float())。


为了额外的功劳,您可以尝试使用Python csv library 来读取和解析您的文件:

# Tell Python that you are going to use the csv (comma separated value) library
import csv

# Open the file
file = open('marks.csv')

# Use the csv library to read the file, instead of using "for line in file1"
markReader = csv.reader(file)

# Using this for means that each line is read as a list of strings. 
for row in markReader:

    # Now we get the string we want from the list. 0 would get the name, 1 gets the mark
    number_as_string = row[1]

    # We now make the string an actual number, because if we add '10' + '20', we get '1020' instead of 30.        
    number = float(number_as_string)

【讨论】:

  • 已修复,谢谢。我改编了图书馆参考示例中的代码。至于作业的问题,教人钓鱼。
  • 我不想让任何人做我的家庭作业 我说我已经编写了大部分程序 我需要帮助,我不明白这是一个 11 周的课程,时间不够学习我需要学习的一切我是个骗子..谢谢
  • 我可以把节目的全部内容发给你看
  • 但是你说的语言对我来说就像中文
  • 我添加了一些您应该使用交互式解释器的更简单的示例代码。它将帮助您理解您的代码以及如何修复它。
猜你喜欢
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 2011-12-04
相关资源
最近更新 更多