Trouble with an if/else inside a function


#1

https://www.codecademy.com/courses/learn-python/lessons/functions/exercises/practice-makes-perfect?action=lesson_resume

File “python”, line 8
else:
^
SyntaxError: invalid syntax

  • I am stuck not sure how to proceed with this syntax error on the else statement
def cube(number):
  number=number**3
  return number
def by_three(number):
    if number%3==0:
      cube(number)
    return number
    else:
    return False
  
  cube(3)
  




#2

If the number % 3 == 0, then return the cube(number). Else, return False.

Now look at your code. You have:

if number % 3 == 0:
   cube(number)
return number

The function cube() returns the cubed number. You are calling it correctly; however, you’re not returning it. Look at your code.

Psst…the return you have in the if and else code blocks need to be indented for it to belong to that code block.


#3

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