Python Practice Makes Perfect


#1


ef cube(number):
if cube == number:
return "number**3"

def by_three (number):
if number % 3 == 0:
return cube (number)
else:
return "False"

Oops, try again.
cube(1) returned None, did you remember to return the result?

`


#2

@dalavigo,
Your cube function
return's a string Value "number**3"
instead of a number Value number**3


#3

Its still giving me the same result.


#4

@dalavigo
Please give the FULL code you are using now...


#5

Change ef to def on the 1st line. Also you don't need an if statement in the cube() function.
You have to declare what number is;

def cube(number):
number = number **3 # this declares that number is equal to (number) x (number) x (number)
return number # this returns number ** 3 because of the line above.
# eg. number = 2
# eg. (number ** 3) = 8
# eg. (2 x 2 x 2) = 8

Now in the second paragraph you have done everything correctly except for the last line; return "False"
For this you must use the Boolean Value called False not the string "False"
The difference is that with return "False" you are trying to return the word False, not the boolean value False, which is another way of saying that the statement is "not true".
In other words the second function reads as:

def by_three(number):
if number % 3 == 0: # if number is divisible by 3
return cube(number) # call function cube and return the value of number.
else: # otherwise
return False #return the boolean value False

Full Code:

def cube(number):
[tab] number = number **3
[tab][tab] return number
def by_three(number):
[tab] if number % 3 == 0:
[tab][tab] return cube(number)
[tab] else:
[tab][tab] return False


#6

I don't remember this one in particular, but some thoughts:
return 3**3 # this will return an integer. In effect: 27. This will be a number.
return str(3**3) # this will return "27". This will be a string representation of a number.
Is that it?


#7

def cube(number):
if number == number**3:
return number**3

def by_three (number):
if number % 3 == 0:
return cube(number)
else:
return "False"
Oops, try again. cube(2) returned None, did you remember to return the result?


#8

def cube(number):
if number == number**3:
return number

def by_three (number):
if number % 3 == 0:
return cube(number)
else:
return "False"

Oops, try again.
cube(2) returned None, did you remember to return the result?


#9

def cube(number):
if number == number**3:
return number

def by_three (number):
if number % 3 == 0:
return cube(number)
else:
return "False"

Oops, try again.
cube(2) returned None, did you remember to return the result?


#10

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