Stuck on python!


#1

https://www.codecademy.com/courses/learn-python/lessons/conditionals--control-flow/exercises/i-got--problems-but-a-switch-aint-one?action=lesson_resume&link_content_target=interstitial_lesson

Have been stuck on this for hours. Don’t know if I misunderstood the instructions or the code.


#2

Go ahead and share the code you have as well as any errors you are getting. That better equips us to guide and nudge you forward.

To share your code, do this:

  1. Click on the </> icon here in the post reply editor. It creates a code block for you with an opening and closing triple back ticks.

  2. Then from the Python lesson, copy your code.

  3. Then paste it in-between those back ticks where it tells you to put your code.


#4

Hi, @hellofromtonya ! I’m suffering with the same lesson. Please, can i send you my code? Do you help me too?


#5
def greater_less_equal_5(answer):
    if 2 < 3 :
        return 1
    elif 2 > 3 :          
        return -1
    else:
        return 0
        
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)


#6

And the feedback is:
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!


#7

Issues with your code:

  • The function is checking if the number passed into the function is > 5 or < 5 (not 3). I’m not sure where you got 3 from in your code.
  • You want to use the function’s parameter, which you named answer to do the comparisons. Why? It makes the function reusable. Notice in the print statements that you are passing in different numbers to be compared to 5.
  • Also, the comparisons are: > 5 return 1, else if < 5 return 1, else just return 0.

Visualization Walkthrough

Let’s walk through the first print statement to help you visualize what’s happening.

  1. Run print greater_less_equal_5(4).
  2. That calls (invokes) the function greater_less_equal_5() and passes the 4 and assigns it to the function’s parameter, you called answer. answer = 4.
  3. If answer > 5, then 1 is returned back and printed.
  4. If answer < 5, then -1 is returned back and printed.
  5. Otherwise, 0 is returned back and printed.

See if you can convert that into a function.


#8

I got it! Thank you very much @hellofromtonya! :+1:


#9

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