14. I got 99 problems, but a switch ain't one


#1

What did I do wrong?

# This is my code:
def greater_less_equal_5(answer):
    if 5:
        return 1
    elif 5:          
        return -1
    else:
        return 0
        
print greater_less_equal_5(6)
print greater_less_equal_5(7)
print greater_less_equal_5(8)

#2

What did I do wrong please help.

Thanks if you did!


#3

This had me scratching my chin for a while haha

Read what they want you to do again.

From your code it looks like "if this is 5, then return 1, but if this is 5 then return -1" - it doesn't add up

You've given it two different outcome options depending on the inputs, but two input checks which are the same!

The first IF should be asking "Is this greater than five"

The second should be asking "Is this less than five"

These are two separate input checks, which then lead to two the return of IN or ELIF or ELSE depending on what the input actually is.


#4

so is it suppose to be like:
if >5:
Print 1
elif <5:
print 0
else:
print -1


#5

if answer >5
print 1
elif answer <5


#7

Oh! Thanks for the help now I get it.:slight_smile:


#8

What?
This has had me stuck for a while and I've given in and went to the Q/A forums and I am a little confused.


#9

I've got the reults but:-
I do not understand how the code arrives at the results.

How did the variable (answer) get set to a number before the if statement in line 2.

And where does answer 3 come in?

So far the course has not covered the def function, unless I missed it somehow.

Can someone please explain the above in non technical terms as the majority of my time is taken up my trying to fathom how the code works.

here's hoping.


#10

I finally understood it. You have 3 print funcionts, each with a different answer. The first one is 4, so the if statement will be false, but the elif will be true, so it will return -1. The second one is 5, so both the if and elif statements will be false and it will return 0. And the third one is 6 so the if statement will be true and it will return 1.


#11

Hi delspy.

Here is how it works, for you too understand properly.

Your code when complete:

def greater_less_equal_5(answer):
    if ________:
        return 1
    elif ________:          
        return -1
    else:
        return 0
        
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

understanding parameters of a function.
The first part of the syntax is def and that's what you write at the beginning of a function, then it is followed by the name of the function it does not play an important role except you use the name when you call the function so it can run all the code inside the functions block.

def greater_less_equal_5(answer):

Now the last part of the syntax is called the parameter it is pretty much like a variable but you will give it a value once you call it so for example this code.

print greater_less_equal_5(4)

in this example print is what you write to print the returned value or string and after that is the name of the function and the the place were you insert a value which you'll use too set the "answer" parameter for the function.

End of explanation.


#12

Thank you so much for the explanation, I was sitting here for good 20 minutes wondering what am I supposed to do, and when I finally managed to do it, I was wondering why does it work that way. Thanks a lot.


#13

here's my answer


#14

Thanks Peter

That was a great help. I got the coding correct at last. I think I will have to do a few more coding examples for 'def' functions to help me remember the syntax etc.

Thanks again.

Derek (delspy)


#15

Thanks for your help, all. They screwed the pooch on the instructions with this one!


#16

Appalling instructions on this one. If the instructions has indicated that we were too use the (answer) as part of the solution it would have made sense, but the example was totally misleading.


#17

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