Receiving this error : n.append(float(score)) AttributeError: 'int' object has no attribute 'append'. After running code below
def averageTestScore(n):
total = 0
for x in range(n):
total = x #sum of numOf tests taken
avg = total / len(str(n))
for counter in range(0, n):
score = float(input("What was your score on test # "))
# n.append(float(score))
# score = counter
return avg
function call:
numberOfTests = int(input("How many tests did you take? "))
average = averageTestScore(numberOfTests)
print("The average of those test scores is:", average)
CodePudding user response:
In your code given above , you were trying to append an integer to another integer (Note n
is the number of tests you had used as a function parameter, that's why you got an Error).
I think what you want is to get average of tests.
So, I think this may help you
def averageTestScore(n):
tt=0
for i in range(n):
sc=float(input("What was your score on test : "))
tt =sc
return tt/n
numberOfTests = int(input('How many tests did you take ? '))
average = averageTestScore(numberOfTests)
print("The average of those test scores is :", average)
CodePudding user response:
Change your code to -
def averageTestScore(n):
score = 0
for num in range(1,n 1):
score = float(input(f"What was your score on test {num} "))
avg = score/n
return avg
numberOfTests = int(input("How many tests did you take? "))
average = averageTestScore(numberOfTests)
print("The average of those test scores is:", average)
Then it should work without error.
You got error, because you cannot append to a integer, only to a list you can append. So, try this different approach. I used f-strings
so that the test number is also shown.