I got 99 problems and a switch aint one of them


#1



Can someone please explain this to me clearly with a code example. I think this particular exercise is not very well written and the hint is as useless as it can be.

I dont need some clever person to write riddle in their answer.. I would be grateful for a clear explanation and a working code example.
So frustrated with this as everything was going ok so far.....


Replace this line with your code.


#2

I'm having an issue as well. I'm not quite understanding the def relationship.

`
def greater_less_equal_5(answer):
if greater_less_equal_5 > 5:
return 1
elif greater_less_equal_5 < 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 keeps returning 1 in all cases. I'm fairly new to coding so I'm having a little trouble figuring out these relations. Can someone help me out with this one?


#3

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

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

I'm an idiot, I figured it out. Just have to set the answer to the conditionals, not the function. Derp.


#4

I still don't understand (a) where and what is the value of answer itself and (b) how do 4, 5, 6 work


#6

All you're doing here is providing the condition for the if and elif statements.

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

This is creating the perimeters for what will be returned for any given int value entered in the below statements.

print greater_less_equal_5(4) #because value in parentheses is less than 5, print -1
print greater_less_equal_5(5) #because value in parentheses is not less than 5 or greater than 5, print 0
print greater_less_equal_5(6) #because value in parentheses is greater than 5, print 1

The code above is testing what values are in the parentheses and returning 1, -1, 0 accordingly. These are entered with a print statement to provide a read out in the terminal.

The return will read something like

-1
0
1

Sorry for the format not matching what it would look like in the actual code. I hope you can still understand this.


#7

i literally just shook my head in anger and shame when I read your solution to this. thanks for figuring it out for the rest of us. happy coding


#8

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