 # How am I supposed to check the value of answer?

## Question

How am I supposed to check the value of 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:
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.

5 Likes

can anyone explain to me why does the answer is -1, 0, and 1?

Could you provide the link/url to exercise? Then i can write a better answer

1 Like

the arguments at function call:

``````# function calls with different arguments (4, 5 and 6)
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
``````

are passed to the parameter:

``````# defining a function with answer parameter
``````

so for the first function call, we have `answer = 4`, which will return `-1` (elif), given 4 is lesser then 5.

I don’t think its a logical answer, as in a mathematical answer or one based in reason, its just what they used. It could have been A, B, and C if they wanted to…

Can you please explain me in detail how -1, 0 and 1 are being displayed in the console?

they are the values returned by the function.

if you think away the function:

``````
print 1
print -1
else:
print 0
``````

does the 1, -1 and 0 make sense then? The great thing about the function is that we don’t have to copy and paste the condition twice

2 Likes

What are some common uses of elif?

if you need to check for multiple conditions, `elif` is very useful

You need to pay attention to this lines of code:
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
Firstly, instead of the value of the variable answer, a numeric value is inserted. In this case we have 4, 5, 6.
Secondly, the condition is checked.

For example:
if 4 > 5:
return 1
elif 4 < 5:
return -1

The first condition is not true (4>5), so the program output true result (-1).
The following steps are the same.

I am trying to take user input of a no. and then trying it to compare with set function. But it is not giving me desired result. Where am I going wrong?? If user Enters -6 then the result should be -1.But its showing 1.
I have used answer as variable.

what data type does `raw_input()` give back?

It’s because raw_input() returns the input as a string, I think.

Would using input() work?
(Just checked, it does:)

"One difference between input() and raw_input(). a = input() will take the user input and put it in the correct type. Eg: if user types 5 then the value in a is integer 5. a = raw_input() will take the user input and put it as a string. Eg: if user types 5 then the value in a is string ‘5’ and not an integer. "

https://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3

https://docs.python.org/3/whatsnew/3.0.html

using `input()` in python 2 is a horrible idea. You know now why `input()` put the “correct” type? Because it evaluates the entered data as code. Thus people can inject code into your program. That does not sound like a good idea

there are better and safer ways to do this.

1 Like

I didn’t realize that the post I replied to specified python 2 which is why my links were in reference to python 3. Thanks for the input.

If they had specified python 2, my answer would have been different.
If it was stated earlier, I must have missed it.

EDIT raw_input() has been renamed input() in python 3(for anyone who’s reading along:P)

But in python3, `input()` produces a string as well: 1 Like

Makes sense. Thanks.

EDIT tbh I forgot there was a mismatch of versions between the tutorial editor and the version I’m using locally.
I was just killing time with the tutorial to get some python practice. Thanks for pointing it out.

Not that the title(Python 2) wasn’t a big enough hint.

print(accuracy_rating(59)) # 59 targets hit - Work on your aim!
print(accuracy_rating(89)) # 89 targets hit - Aw, shux.

What a rigged game you have there, LOL!

I believe this would give some better results? ``````def accuracy_rating_2(targets_hit):
if targets_hit >= 90:
return "Super accurate!"
elif targets_hit < 90 and targets_hit >= 60: