Review functions (module - 17/19)


#1

the question -

First, def a function, shut_down, that takes one argument s. Don’t forget the parentheses or the colon!

Then, if the shut_down function receives an s equal to “yes”, it should return “Shutting down”

Alternatively, elif s is equal to “no”, then the function should return “Shutdown aborted”.

Finally, if shut_down gets anything other than those inputs, the function should return “Sorry”.

my doubt:

  1. What the heck did just happen. i was just printing out “hello world!” and suddenly this!
  2. please can someone help me understand this exercise?

#2

Was that in the instructions? As indicated above, it would appear not. The lessons are very strict in what is to be expected of the learner, so taking liberties away from the lesson instructions will ultimately result in whatever this is.

First, define a function, shut_down

def shut_down():
    # code block

It is to take one argument, s

def shut_down(s):
    # code block

Now what to do with the parameter once it gets inside the function? Check its value…

    if s == 'yes':
        # do something
    elif s == 'no':
        # do something else
    else:
        # do the fallback default action

The do something will be to return the appropriate phrase.


#3

firstly, thank you for the prompt reply.

when i was finding my way around, this is what i did( i tried using the help of the given example):
def shut_down(s):
if yes():
return “shutting down”
elif no():
return “shut down aborted”
else:
return “sorry”

and apparently this is fine. when i ran the code it did the trick.
how is this different from what you just said?


#4

That ignores s, can’t possibly do what’s asked


#5

That ignores s, can’t possibly do what’s asked

this is exactly my point. probably some bug in the software?


#6

If that passes then yes codecademy is to blame (and it wouldn’t be anything new)

I’d test anything I write so that I can see for myself how my code behaves
not that it excuses codecademy, but it’s a good idea regardless, to see the effects


#7

Hello, I also have some trouble with the module 17, “Review: Functions”. I think I did the function as requested but had the following error:
File “python”, line 5
SyntaxError: ‘return’ outside function

I checked but can´t find out why it says that, I can´t see the mistake appearently because for me it seems that the return is in the function.
Here is my code:

def shut_down(s):
  return s

if s == "yes":
  return "Shutting down"
elif s == "no":
  return "Shutdown aborted"
else:
  return "Sorry"

I would really appreciate any help


#8

The first line inside the function effectively terminates it before anything happens. Remove that line, and be sure to indent the rest, as applies.


#9

Thank you so much! I indented the rest as you said and it fixed it


#10

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