Review: Functions


#1



The error is:

File "python", line 3
if s == "yes":
^
IndentationError: expected an indented block

What's wrong here with my code?


def shut_down(s):

if s == "yes":
    return("Shutting down")

elif s == "no":
    return("Shutdown aborted")

else:
    return("Sorry")


#2

You define the function, but the current indentation sets those if/elif statements outside of the function so the function is empty. What indentation would be required to get those statements inside the function?


#3

def shut_down(s):
if s == "yes":
return "shutting down"
elif s == "no":
return "shutdown aborted"
else:
return "sorry"

I put it one line higher but it still doesn't work.


#4

your code is all correct, but you need to indent the if/elif/else statements. here's an example:

def shut_down(s):
    if s == "yes":
        return("Shutting down")

see how the if statement is further in from the left margin? it's one [tab] in, and then the return("Shutting down") is indented another [tab] in from that. (when i say [tab] i mean you're hitting the tab key.) Keith Shannon's response to this Quora question explains why indentation is important really well: https://www.quora.com/Why-is-indentation-so-important-in-Python.

-frnki


#5

Still doesn't work. I'm not sure if this is a website issue or if there is actually something wrong with the code.


#6

hmm, it works for me when i indent it like that... what output are you getting?


#7

I get an indentation error.


#8

Even after copying and pasting this code, it still doesn't work.


#9

def shut_down(s):

    if s == "yes":
        return("Shutting down")

    elif s == "no":
        return("Shutdown aborted")

    else:
        return("Sorry")

That should be the correct indentation. The environment codecademy uses automatically sets the indentations, so avoid deviating from where the software places you.


#10

2 posts were split to a new topic: Review: function


#11

A post was split to a new topic: Review: functions


#12

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