How am I supposed to check the value of answer?


#1

Question

How am I supposed to check the value of answer?

Answer

answer is a variable that we’re able to use because it’s provided to the function when the function is used. You’ll learn about the syntax of functions in due time, but you already know how to use variables!
It’s totally fine that we don’t know the value of answer ahead of time - that’s what makes functions so useful and reusable!
If we wanted to check if answer was equal to 5, we could write if answer == 5:. All we need to do, then, is compare answer to 5 using the operators we’ve learned about so far for greater and less than.
If we had a separate but similar problem where we had a function that was given a variable called targets_hit and wanted to check for two different values to assign an accuracy rating, we might do that like this:

def accuracy_rating(targets_hit):
    if targets_hit > 90:
        return “Super accurate!”
    elif targets_hit < 60:
        return “Work on your aim!”
    else:
        return “Aw, shux.”

Even though we have no idea what the value of targets_hit is beforehand, we can still write our conditions because we do know what we want the value to be for certain responses.


FAQ: Learn Python - Conditionals & Control Flow - I Got 99 Problems, But a Switch Ain't One