Functions That Decide โ๏ธ
Now mix return with if and your function becomes a little brain that
gives different answers for different inputs.
def judge(score):
if score >= 90:
return "๐ฅ Gold!"
elif score >= 70:
return "๐ฅ Silver!"
else:
return "Keep practicing!"
print(judge(95)) # โ ๐ฅ Gold!
print(judge(72)) # โ ๐ฅ Silver!
print(judge(30)) # โ Keep practicing!
Remember: the moment a return runs, the function stops right there
and hands that answer back. The other branches never even get looked at.
The big rule: make sure every path has a return. If a score sneaks
through with no return, your function hands back None โ Python's way of
saying "uhh, nothing?"
Your mission
You're judging a talent show.
- Build a function
judge(score)with one parameter - Use
if/elif/elseinside it, with these cutoffs:- 90 or more โ return a top-prize verdict
- 70 or more โ return a middle verdict
- anything lower โ return an encouraging message
- Call it at least 3 times with different scores and print each verdict
The words are yours โ make the verdicts funny! ๐ค
Your code
Press โถ Run to see what your code does!