Python


#1

Ok so is it me or is the compiler really picky about things? I put in my code and it kept returning me an error as follows;

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

The top block of code is mine while the bottom block is the solution provided to me after numerous failed attempts.

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




def cube(number):
  return number * number * number

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

#2

… and the indentation is all awry.


#3

The code in your post is valid syntax. Most likely you inadvertently changed the code while pasting/editing

It’s not about pickiness. There’s no such thing as almost right. If something is in some way wrong then you’re lucky if it doesn’t run, it would be worse if it ran because then it would go undetected.


#4

I believe unlike other languages, python uses the indentation for nesting a block of code.
So i think everything else looks generally fine except you tabbed your return cube(number) a little bit to far.


#5

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