有人可以告诉我编码有什么问题吗?我必须
代码:
f=open("greades.txt", "r")
sum=0
for line in f:
#split data into rows
items = line.split()
sum=0
#getting the data in rows
for i in range(1,9):
sum+=int(items[i])
print(items[0],"\tTotal:",sum,"\tAverage:",sum/9)
def average(mygreades):
""" Function to calculate the average of an input of the List that i have """
if(len(mygreades)==0):
return 0.0
mygreades=[1]
sum=0
for item in mygreades:
sum+=item
avg = sum/len(mygreades)
return avg
def converter(mygreades):
""" Function will convert an input list of strings to a number list """
numberList = []
for item in mygreades:
if(item.isnumeric()):
numberList.append(eval(item))
return numberList
main()
答案 0 :(得分:1)
您有缩进错误。请考虑以下代码行:
def average(mygreades):
""" Function to calculate the average of an input of the List that i have """
if(len(mygreades)==0):
请注意文档字符串和if
语句是如何在def
下完全排列的。这是不正确的缩进 - 函数的主体必须缩进。当你在这里做一个循环在循环中的函数时,这也有点不寻常。