Review : function


#1

def shut_down(s):

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

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

please help


#2

Make sure you are indenting properly!
Everything that is nested needs to be indented.

def function(n):
    if #condition:
        return #something
    elif #condition:
        return #something
    else:
        return #something

#3

didnt help
tried everything


#4

solved. thanks datfatcat


#5

how?

i tried everything


#6

Share what you've done and we can help.


#7

def cube (number):
return n**3
def by_three (number):
if by_three % 3:
return cube(number)
else:
return False

My line 6 seems to have syntax error...


#8

Ah...different lesson. Ok....for this function:

def cube (number):
    return n**3

You defined number to be your input variable....why aren't you using that? n is undefined (what's n?).

As for this:

def by_three (number):
    if by_three % 3: 
    ... rest of your code ...

Why are you using the function name? Your input variable is number.
Also, your if statement is incomplete. What about the remainder of the number divided by 3? Does it have to be equal (==) to something?


#9

I'll be back differently


#10

def cube (number):
return n**3
def cube (number):
if n % 3 == 0:
return "n is divisible by 3"
else:
return "n is not divisible by 3"

Still probs on line 6


#11

what error is it saying ?
share the error


#12

File "python", line 7
else:
^
SyntaxError: invalid syntax

def cube (number):
output = number * number * number
return output
def cube (number):
if n % 3 == 0:
return cube (number)
else:
return False


#13

Why did you change it? This is more redundant.
Why not just do return number ** 3???

Like I said, your input variable is number. Where does n come from??
Also why does this function have the same name as the previous one? In the instructions, they said to create two functions with different names. One is cube and the other is by_three.

Please check your indentation as well.
example:

def function1(var):
    return "foo"

def function2(var):
    if var % 3 == 0:
        return function1(var)
    else:
        return None

#14

what is fooo?
it doesn't make sense


#15

better?

def cube (number):
return number ** 3
def by_three (number):
if number % 3 == 0:
return cube (number)
else:
return None

i still get error on lin 6 syntax


#16

Looks better.
I'm not providing you the correct code. I'm providing you an example of how it should look like. foo is just a placeholder for anything.
Your else case should return False as per the instructions (please read the instructions).
Your indentation should look like this:

def cube (number):
    return number ** 3 
def by_three (number):
    if number % 3 == 0:
        return cube (number)
    else: 
        return None #Make None to False

#18

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