Functions!


#1

def shut_down(s):

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

Here is my code and I am not able to understand what is wrong with it. it returns the following error:

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

Please help me! :)))


#2

There is a very important concept in python called "Indentation" (Spaces)
We use it to explicitly define different blocks of code,child blocks of a parent block etc.

suppose there is a function ..

def someFunction():
    #someCode
    #someMoreCode

You can see I have spaced out the internal code by some spaces(usually we use 4 spaces).

This means that #somecode and #someMoreCode blocks are inside(part of) the function.

In your case the if/else should be inside the function shut_down()


#3

Hey.
Thank you for your reply. This is my code now, but it is still returning the error

def shut_down(s):

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


#4

I have added the space but it is getting cut while replying!!!!!!!!!!!!!!:sweat_smile:


#5

Oh it still not indented,I will give a skeleton of it.

def someFunction():
    if test :
       # yourcode
    elif test :
        #yourcode
    else: 
        #yourcode

Look carefully how if/else is child of function so its indeneted by 4 spaces, also the #yourcode is part of if/elif/else so they are spaced out in same manner(by 4 spaces)?


#6

Thank you. Finally got how to do it! :grin:


#7

I suggest you to take a look at these articles. (so you deal with future indentation mistakes)

Quora - Indentation

A very good Answer from old CC forum.


#8

Sure, Thanks a lot)))


#10

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