Just Weight and See - get_average(alice) raised the following error: string indices must be integers, not str


#1

Though the function works fine and it prints the graded_average but the
validator returns “string indices must be integers, not str” while output
printed correctly!

output


#2

First of all you should get rid of the variable total and the for loop and turn that into one line of code:

total = sum(numbers)

Then instead of making the variable result you could just do:

return total / len(numbers)

That is the first function done. Now the second function you should get rid of the variable total_average and the for loop underneath it and the print command underneath that. After you've done that you should change the 3 variables homework, quizzes and tests. You should change the i to the argument of the function 'student'. Do this for all 3 variables. Then get rid of the variable total_average near the end of the function and change that to one line of code:

return 0.1 * homework /
0.3 * quizzes /
0.6 * tests

You use the slashes because you have to do each line on its own and then add them up. The slashes basically continue the code you are writing onto a new line. If you don't want to use the slashes you could just add brackets like this:

return (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)

After you have done all those changes you should get rid of these 4 lines of code:

    print "total_average%5.2f" %(total_average)
return "total_average%5.2f" %total_average

all_students=[alice,tyler,lloyd]
get_average(all_students)

After that try running your code to see if it works.


#3

Oh and also get rid of the line of code:

total += i

in the first function


#4

this is how it should look like. all credit to maemoazzam


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.