If statement, can't figure it out?


#1



https://www.codecademy.com/courses/python-beginner-BxUFN/3/4?curriculum_id=4f89dab3d788890003000096#


It's telling me that my statement is returning 1 instead of -1 and answer is 3...?


if 10 >= 5:
return 1
elif 5 > 1:
return -1
else:
return 0

Its telling me that I'm returning 1 instead of -1, so I'm assuming the elif statement is wrong somehow but it says this.
function output is returning 1 instead of -1 when answer is 3. (where do they get 3 from !?! )


#2

Is 10 greater than or equal to 5?

Yes... So the first branch runs, returning 1, instead of -1. Only the first branch that successfully works will run. Elif statements are only accessed if the original if doesn't pass.


#3

So I need to make the if statement false?


#4

In order to make your statements return -1, your first branch, in this case, needs to be false, yes.


#5

I tried that, I did if 5 > 10
elif 5 < 10
else:
return 0

its telling me now that I'm returning -1 instead of 0 when answer is 5.... where do they get this "answer is 3, answer is 5" from?


#6
Imgur

I found someone elses topic and found their answer, but I have 0 understanding of how they got it.


#7

It seems you're missing the purpose of this lesson...

This lesson serves to teach functions that take parameters. For example, if we define a function greater_less_equal_5 with the parameter answer then we need to check the parameter, against 5, like this:

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

Then we can call it with various inputs:

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

We are taking using input, here. As programmers, we can't always dictate everyone's thoughts, or what they want. We need to be able to compare a wide range of numbers.


#8

After seeing the end product I understand, it wasn't clear to me that I was comparing answer against the

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


#9

When you're printing each of those out, you're comparing 4, then 5, then 6 to 5. You'll get

-1 (4>5? No, 4<5? Yes so return -1)
0 (5>5? No, 5<5? No, 5=5? Yes so return 0)
1 (6>5? Yes so return 1)


#10

Right, I was completely ignoring the print at the bottom... that's what I wasn't getting haha.

Thanks for your help, I'm sure you'll see me again lol...


#11

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