# Just Weight and See

#1

Here's my code:

def average(numbers):
total = 0.0
total += sum(numbers) #add the number in numbers all together from the first one to the last one
total /= len(numbers) #divide the sum of the numbers by the length of the "numbers" list
print total

def get_average(student):
score = 0
for person in student:
score = 0.1 * average(student["homework"]) + 0.3 * average(student["quizzes"]) + 0.6 * average(student["tests"])
print score
return score
get_average(guys)
guys = [lloyd, alice, tyler]

Traceback (most recent call last):
File "python", line 34, in
File "python", line 31, in get_average
TypeError: list indices must be integers, not str

why can't I use the "for loop" to print out all of the three student's grades?
I can print their grade individually but at the bottom, the "guys" list, it seems to be useless
and what dose "list indices must be integers, not str" means?
how can I fix it?
Thanks a lot for your help!

``Replace this line with your code.``

#2

Guys this is the code and getting errors, kindly rectify the error.

## error: Traceback (most recent call last):

File "python", line 32, in
NameError: name 'guys' is not defined

## program

lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

def average(numbers):
total = sum(numbers)
total = float(total)
result = total / len(numbers)
return result

average([1,2,3])
def get_average(student):
for boy in student:
total=0.1*average(student["homework"]) +0.3* average(student["quizzes"])+ 0.6*average(student["tests"])
print total
get_average(std)
std=[lloyd,alice,tyler]

#3

Dear betablaster23908,

i found the error:
only add whitespace in last two lines becz that is the part of for loop:
You can copy my function code and replace the 'std' with your 'guys' word

def get_average(student):
for boy in student:
total=0.1*average(student["homework"]) +0.3* average(student["quizzes"])+ 0.6*average(student["tests"])
print total
get_average(std)
std=[lloyd,alice,tyler]

#4

Thanks again for your generosity, sorry I was a beginner in programming, maybe my problems seem a bit stupid.
As I ran your code, the monitor showed that "std is not defined"

Following codes are which I tried just a moment ago. These can help me print all the results I want on the monitor,
but the website kept telling me that "get_average(alice) raised the following error: string indices must be integers, not str", and I don't know why..

``````def get_average(student):
for person in student:
print person["name"]
score = 0.1 * average(person["homework"]) + 0.3 * average(person["quizzes"]) + 0.6 * average(person["tests"])

print "Total score is : %s" % score
return score
get_average(guys)
guys = [lloyd, alice, tyler]``````

The monitor will show this:
Lloyd
88.5
74.0
82.5
Total score is : 80.55
Alice
97.5
85.3333333333
93.0
Total score is : 91.15
Tyler
46.0
51.0
100.0
Total score is : 79.9
None
and these are almost the result I want, except the numbers which are not total score
can you teach me how to remove those numbers?
`

#5

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