Just Weight and See


#1



Just Weight and See. Student Becomes the Teacher exercise 6.


I keep getting the error code:
File "python", line 27
get_average(student):
^
SyntaxError: invalid syntax


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]
}

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    i = total / len(numbers)
    return i
    
get_average(student):
    homework = average(student[homework])
    quizzes = average(student[quizzes])
    tests = average(student[tests])
    grade = 0.1 * homework + 0.3 * quizzes + 0.6 * tests
    return grade


#2

Hi @arcplayer47661,

Since it is intended as a function header, this ...

get_average(student):

... should be ...

def get_average(student):

#3

Yeah, I fixed that mistake but, it still returns this in the console

File "python", line 28
def get average(student):
^
SyntaxError: invalid syntax


#4

This is incorrect because it has a space in the name of the function ...

def get average(student):

#5

Hey guys! Was having trouble with this too, but just got through it.

In addition to what appylpye said about the "def get_average(student):"

you also need to add parentheses to the names in the brackets.

this:

should be:

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
grade = 0.1 * homework + 0.3 * quizzes + 0.6 * tests
return grade


#6

Thanks, @kpkeila, for your response.

It is actually quotes, rather than parentheses, that @arcplayer47661 is missing here ...

    homework = average(student[homework])
    quizzes = average(student[quizzes])
    tests = average(student[tests])

However, you are correct, @kpkeila, that this ...

    homework = average(student[homework])

... must be changed to this ...

    homework = average(student["homework"])

... and that the other two lines need to be corrected in a similar manner.


#7

say, I ran this code and it worked:

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
fngrd = ((0.1*homework)+(0.3*quizzes)+(0.6*tests))
return fngrd

but I want to know why this works:

 fngrd   = ((0.1*homework)+(0.3*quizzes)+(0.6*tests))

But this won't:

fngrd =  ((homework*.1)+(quizzes*.3)+(tests*.6))

#8

Add the keyword 'def' before your function definition get_average(student)

The correct syntax is as follows:

def get_average(student):

Happy coding!


#9

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student(["tests"])
grade = ((0.1 * homework) + (0.3 * quizzes) + (0.6 * tests))

return grade

I is showing error invalid syntax at grade.. Can anyone tell where the mistake is??


#10

Thank you. That I understand. But if I literally swap out the two lines I asked about, and the function starts with the "def" keyword, the code appears to run differently. Could you please help understand why that may be? I really want to understand.


#11

Hi @bitsolver35607,

This should work ...

fngrd =  ((homework*.1)+(quizzes*.3)+(tests*.6))

... unless you changed something else in addition to that line within your get_average function definition, or changed something elsewhere in your code that affected the results.


#12

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