(Beginner) Python Codeacademy Functions

I was just learning to code on Codeacademy. I have a task, but I can’t understand what I am doing wrong.

First I need to define a function that returns a cube of value. Then I have to define a second function that checks if the number is divisible by 3. If necessary, I must return it, otherwise I need to return False .

the code looks like:

 def cube(c): return c**3 def by_three(b): if b % 3 == 0: cube(b) return b else: return False 
+4
source share
6 answers

You do not catch the return value of the cube function. Make b = cube(b) . Or better yet, do return cube(b) .

 def cube(c): return c**3 def by_three(b): if b % 3 == 0: b = cube(b) return b # Or simply return cube(b) and remove `b = cube(b)` else: return False 

When you call the cube function with argument b , it returns the cube of the passed argument, you need to save it in a variable and return it to the user, in your current code, you neglect the return value.

+8
source

I think this answer may also work:

  def cube(b,c): b = c ** 3 if b % 3 == 0: return b else: return False return b 

I know this may be a little redundant, but I think this may be another way to do what you are trying to do. Sucrit, in my opinion, is easier.

+2
source

I finished Codecdemy and this is my code.

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

Here the easy solution that I just developed should work :)

 def cube(number): return number**3 def by_three(number): if number % 3 == 0: return cube(number) else: return False 
0
source
 def cube(num): return n ** 3 def by_three(value): if value % 3 == 0: return cube(value) else: return False 
0
source

try specifying the same letter for each. Instead of using 'c' and 'b' just use 'c'

 def cube(c): return c**3 def by_three(c): if c % 3 ==0: return cube(c) else: return False 
0
source

Source: https://habr.com/ru/post/1494986/


All Articles