7 Sending a Letter


#1



I am stuck on exercise Plan Your Trip


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)
    total = float(total) / len(numbers)
    return total

def get_average(students):
    homework = average(students["homework"]) * 0.10
    quizzes = average(students["quizzes"]) * 0.30
    tests = average(students["tests"]) * 0.60
    avg = homework + quizzes + tests
    return avg
    
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))

why when you do the if statement does it only return a B, the statement should return B,C,D because isn't 80 >= 70
and isn't 80 >= 60. so why does it only return a B


#2

i didn't uderstand very much your question , but i will try ; so if score = 80 , first will check (if score >= 90) if 80 >= 90 and that not true so second will check (elif score >= 80:) 80>=80 so this true so it will return "B" and exit the function. ( function can return one statements , in our example return "B")
that what i know if there anything else let me know , thanks


#3

thanks for the help you did answer my question, sorry for being not clear. i didn't realize that an if statement can only be ran once and only return once. again thanks for the help


#4

there are two things which explain this, first off, if a if condition evaluates to true, a elif or else will never run. If/elif/else statements are build this way.

The second thing, is that once a return keyword is reached, something gets returned and the function ends.


#5

hi could you please check my error....

here is my code for this exercise,
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)

average = float(total) / len(numbers)
return average

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

'''
Multiply the 3 averages by their weights and return the sum of those three. Homework is 10%, quizzes are 30% and tests are 60%.
'''
def get_letter_grade(score):
if score >= 90 <=100:
return "A"
elif():
score >= 80 <= 89
return "B"
elif():
score >= 70 <=79
return "C"
elif():
score >= 60 <=69
return "D"
else:
return "F"

print (get_letter_grade(get_average(lloyd)))

I get
Oops, try again. get_letter_grade(89) returned 'F' instead of 'B' as expected.

What am i doing wrong?


#6

your elif() looks wrong, first off, no need for brackets in python, and elif and the condition should be at the same line:

elif score >= 80

also, if you want to check multiply conditions (no need, but you may of course), use the and operator:

elif score >= 80 and score <= 89

but elif will never run if if is true


#7

Thank you for your reply...i corrected it but now i get another syntax error....
Here is the corrected version

def get_letter_grade(score):
    if score >= 90 and <= 100:
       return "A"
    elif score >= 80 and <= 89
        return "B"
    elif score >= 70 and <=79
        return "C"
    elif score >= 60 and <=69
        return "D"
    else:
        return "F"

print (get_letter_grade(get_average(lloyd)))

here is the error 
File "python", line 39
    if score >= 90 and <=100:

SyntaxError: invalid syntax

do i need colon at the end of every elif statement? I am confused...

thank you for your help,


#8

silly me, if i give an explanation, i should include a good example. Take another look at my answer, as you can see you need to compare score with the number at both sides of the and operator, or leave the and operator out all together