Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Making Decisions with if
if runs a block of code only when a condition is true. elif ("else if") adds another condition, and else catches everything left over.
def water_state(temp_c):
if temp_c <= 0:
return "solid"
elif temp_c < 100:
return "liquid"
else:
return "gas"Two things to internalise:
return exits the function immediately. Nothing below a return that ran will execute.The comparison operators are == (equal), != (not equal), <, <=, >, >=. Note that == compares, while a single = assigns. Using = in an if is a syntax error.
Your task:
Write letter_grade(score) that turns a score out of 100 into a letter:
| score | grade |
|---|---|
| 90 or above | "A" |
| 80 to 89 | "B" |
| 70 to 79 | "C" |
| below 70 | "F" |
The boundaries are inclusive at the bottom: 90 is an A, 89 is a B.
Example Tests
95 is comfortably an A
Input: {"score":95}
Expected: "A"
90 exactly is still an A, the boundary is inclusive
Input: {"score":90}
Expected: "A"
89 falls to a B
Input: {"score":89}
Expected: "B"
def letter_grade(score):
"""
Convert a numeric score to a letter grade.
Args:
score: a number from 0 to 100
Returns:
"A", "B", "C" or "F"
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests