FAQ: Functions - Review: Functions

The example is just that, an example. Your program should have a very different structure. return can only be used in the body of a function.

1 Like

S is not defined, yet i’m missing how the variables were defined in the examples. What should I be looking at?

Currently you have an identity function…

def shut_down(s):
    return s

Then you have some code that includes the variable s which was defined not in global scope, but in the function. It will surely raise an error.

How do we get that code into the function, before the return statement, and also ensure the return value is correct for our purposes?

1 Like

Hi there,
Just playing around with this exercise:
I’ve got two codes here - one works but the other doesn’t…It would be great to get some help understanding this! Thank you!

The code (indents don’t seem to show up):

def shut_down(s):
if s == “yes” or “Yes” or “Y” or “y”:
print “Shutting down”
elif s == “no” or “No” or “n” or “N”:
print “Shutdown aborted”
else:
print “Sorry”

shut_down(“no”)

This prints “Shutting down”, but the argument I inputted was “no”. Why is this!?

This alternative version of the code works perfectly:
def shut_down(s):
s = s.lower()
if s == (“yes”):
print “Shutting down”
elif s == “no”:
print “Shutdown aborted”
else:
print “Sorry”

shut_down(“No”)

I should add that, with the first example, if I replace every or with and then the code does work. But this makes no sense to me either! In an earlier lesson, and and or were defined as:
" * and , which checks if both the statements are True ;

  • or , which checks if at least one of the statements is True ;"

So based on this above definition, or seems like the more logical choice than and surely?..

Thank you for your help!
Kind regards,
Alex

Ah i half just figured it out!!:

def shut_down(s):
if s == “yes” or s == “Yes” or s == “Y” or s == “y”:
print “Shutting down”
elif s == “no” or s == “No” or s == “N” or s == “n”:
print “Shutdown aborted”
else:
print “Sorry”

This now works perfectly.

def shut_down(s):
if s == “yes” and s == “Yes” and s == “Y” and s == “y”:
print “Shutting down”
elif s == “no” and s == “No” and s == “N” and s == “n”:
print “Shutdown aborted”
else:
print “Sorry”

This just prints “Sorry” whatever you write. So that explains I was correct to want to use or, rather than and, I just hadn’t written the correctly!

But I still don’t understand why and works when I wrote it incorrectly!?

Using and means all of the conditions have to be true. Which can’t be. How can one string equal multiple different strings?

can somebody tell me why my else: is not being accepted?

you should follow pep 8 style guide. Is not mandatory, but recommend. This style guide included the uses of 4 spaces for indention.

looking at your code, the else keyword isn’t nested inside the function. So its after the function, not after the elif where it should be

4 Likes

thank you, this really helped

1 Like

thank you/ i had the same question and it is works/ but i still can not understand why and how

Can you show what you did first, and what you did to fix it? So I can see where you struggle in your understanding and I can provide an explanation.

yes sure/ there was the same as he did. similar

In python 2 training exercise, I did the following:

def shut_down(s):

  return s

  if s == "yes":

    return "Shutting down"

  elif s == "no":

    return "Shutdown aborted"

  else:

    return "Sorry"


This seems to have run into a loop.  Any thoughts? I'm sure it's the syntax of the def, but there's no clear example of how to properly define functions.

Thanks

That will keep the function from ever running since it exits immediately upon entry.

Why yes, it would do that. I was looking at the “example”, but I’d been away for a bit. :blush:

1 Like

On this exercise I don’t understand their example code:

def speak(message):
** return message**


if happy():
** speak(“I’m happy!”)**
elif sad():
** speak(“I’m sad.”)**
else:
** speak(“I don’t know what I’m feeling.”)**

They don’t define happy() or sad() and I don’t know what their intention is with this code. It also doesn’t run. I made my own version as follows:

def speak(message):
** return message**


def feelings(feeling):
** if feeling == “happy”:**
** return speak(“I’m happy!”)**
** elif feeling == “sad” :**
** return speak(“I’m sad.”)**
** else:**
** return speak(“I don’t know what I’m feeling.”)**

But I don’t know if I’m missing some interesting python feature they were trying to show. Any thoughts?

Could you link the exercise, it seems different to previous posts ta.

This is the link:
https://www.codecademy.com/courses/learn-python/lessons/python-functions/exercises/review-functions

I’m looking at the example code, not the code for the exercise.

No your first instinct was right, on its own this code would invalid as there’s no callable happy or sad defined anywhere :slightly_smiling_face:. Sometimes with pseudocode folks will just act like the components are all in place to focus on a particular point.

It’s a nice example of programming in that even without seeing a definition you have a pretty good idea about what the happy() or sad() calls would return but without a definition it would of course run into a NameError.

Your example makes sense to me but if you want to post code to the forums see How do I format code in my posts? which adds nice formatting and more importantly for Python keeps the whitespace indentation :+1:.

1 Like

I got it to work here, on Python 2 as follows:

def shut_down(s):
if s == “yes”:
return ‘Shutting down’
elif s == “no”:
return ‘Shutdown aborted’
else:
return ‘Sorry’

print shut_down(“yes”)

#Then I got it to work on python 3 as:

def shut_down(s):
if s == “yes”:
return print(“Shutting down”)
elif s == “no”:
return print(“Shutdown aborted”)
else:
return print(“Sorry”)

shut_down(“no”)