【发布时间】:2022-01-08 04:15:39
【问题描述】:
我是 python 的初学者。我有一个从我的计算机读取文本文件并将所有文本转换为 int 的代码。我正在为分析高尔夫比分功能的最后几行而苦苦挣扎。
我需要告诉代码,对于低于 280 的分数来获取这些值并获取数量,我知道使用 Len(score) 来计算,但我首先获取值时出错了。它应该打印低于 180 的分数,但我不断收到错误,我很迷茫!任何帮助都将不胜感激!非常感谢!!!
我的错误出现在最后六行代码中!我不知道如何让它读取列表中 280 以下的值:(
错误是:
TypeError: '<' not supported between instances of 'str' and 'int'##
上线:
if score < 280:
def open_and_read_golf_scores():
raw_scores = open("golfscores.txt", "r")
scores = []
for current_line in raw_scores:
values = current_line.split(",")
scores.append(values[2])
raw_scores.close()
return scores
def analyze_golf_scores():
scores = open_and_read_golf_scores()
total = 0
for score in scores:
score = score[0:3]
total = total +int(score)
ave = total/len(score)
print("Average score =", ave)
for score in scores:
if score < 280:
scores.append(values)
below_par = total + len(score)
print("The number of scores below par is ", below_par)
【问题讨论】:
-
您的缩进看起来不对——不清楚是否存在复制+粘贴错误,或者这是否是您遇到问题的根源。请查看并编辑您的问题以包含您正在运行的确切代码和您收到的确切错误。
-
我已经编辑了代码x
-
现在没有足够的代码来运行——不清楚
scores或values是什么。而且你还没有包括你的错误。 :( -
等一下,我的坏事我把它修好
标签: python list function split python-3.5