Python 14 - if/elif problem 14


#1

On line 2, fill in the if statement to check if answer is greater than 5.

On line 4, fill in the elif so that the function outputs -1 if answer is less than 5.

def greater_less_equal_5(answer):
if 8>5:
return 1
elif 8<5:
return -1
else:
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

It looks like your function output 1 instead of -1 when answer is 3. Make sure you filled in the if and elif statements correctly!

The hint is not helpful at all.

what am I doing wrong?


#2

Hi @jeffrobombero,

Let’s look at your if/ elif statements. I’ll snip out one for you:

When comparing, we need to compare with our parameter answer. Reread the instructions or check the hint in the lesson. :slight_smile:


#3

Make sure the if and elif statements end with colons :

Your code should look something like:

if EXPRESSION:

do something

elif OTHER EXPRESSION:

do something

else:

do something

The hint isn’t very helpful.


#4

Well, did you read what I posted above?

e.g

def my_function(parameter): #see our parameter, parameter?
   if parameter > 5: #here, we check if parameter is greater than 5.
       return 34 #then the function ends when we use the return keyword.

Our parameter answer is sort of a placeholder when we insert argument/s when calling it, like how we called it when printing:


#5

def greater_less_equal_5(answer):
if 8<5:
return 1
elif 3<5:
return -1
else:
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

It looks like your function output -1 instead of 0 when answer is 5. Make sure you filled in the if and elif statements correctly!

So I made the first one false, it goes to the second one which is true and I get the -1, but that is still wrong.

If I change it again,
def greater_less_equal_5(answer):
if 8<5:
return 1
elif 8<5:
return -1
else:
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

It looks like your function output 0 instead of -1 when answer is 3. Make sure you filled in the if and elif statements correctly!


#6

Let’s focus on these two statements…

Did you read the instructions?

…to check if answer is greater than 5.

They’re pretty easy to understand.


Our function is pretty much useless if we’re not going to do anything with our parameter answer.

So really, we need to check if answer is greater > than 5.

Check out my example above :slight_smile:


#7

Let me see if I can nudge you a little further,@jeffrobombero, using what @intermediategamer has laid out for you.

Let me ask you a question. Ready? Why are you hard coding 8 in your function? Why?

The function is receiving the number that the function is supposed to compare to 5. That number gets stored in the variable parameter you called answer.

When you do print greater_less_equal_5(4), you are passing in a 4 to the function in order to compare that 4 to 5 and get the result back.

Now look at what @intermediategamer laid out for you.

Think about how you are reusing this function over and over again to compare different numbers to 5. You don’t want to hard code in 8 as the parameter’s value. Rather, use the parameter that you defined for that function.


#8

Thank you very much @hellofromtonya!
I was just following the previous example and format used on previous problems.


#9

Well, that makes sense as they were setting you up to think about the problem the function is solving.

Did you get it working for yourself?


#10

Yep, sometimes it’s good to go back and review the previous lessons (and look at the examples here), just to get a better understanding on things. :slight_smile: So is your code working then?


#11

It works now and I can move along.
Thanks


#12

Yep, you’re welcome! Happy coding :slight_smile:


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.