Magic 8-ball = Syntax Error: invalid Syntax

Dismiss this post please

Hey, I’m having issues with this project, the error I get points ^ at the letter f in the elif statement but I have no idea what’s causing the issue.

import random
name = 'Me'
question = 'Can dogs fly?'
answer = ''
random_number = random.randint(1, 9)
print(random_number) #CommentOut
if random_number == 1:
  print('Yes - definitely.')
  elif random_number == 2:
    print('It is decidedly so.')
    elif random_number == 3:
      print('Without a doubt.')
      elif random_number == 4:
        print('Reply hazy, try again')
      elif random_number == 5:
        print('Ask again later.')
        elif random_number == 6:
          print('Better not tell you now.')
          elif random_number == 7:
            print('My sources say no.')
            elif random_number == 8:
              print('Outlook not so good.')
              elif random_number == 9:
                print('Very doubtful.')
                else:
                  answer = "Error"

This is what it outputs:

File "magic8.py", line 9
    elif random_number == 2:
       ^
SyntaxError: invalid syntax

I would really appreciate some help, thanks!

Remember that indentation is very important in Python, as it uses it to tell were codeblocks are.
In an if/elif/else, all the ifs, elifs, and elses need to have the same indentation for the interpreter to associate them with each other.

1 Like

I retyped the entire code with proper indentations as I was copy/pasting before, here it is:

import random
name = 'Me'
question = 'Can dogs fly?'
answer = ''
random_number = random.randint(1, 9)
print(random_number) #CommentOut

if random_number == 1:
  print('Yes - definitely.')
  elif random_number == 2:
    print('It is decidedly so.')
    elif random_number == 3:
      print('Without a doubt.')
      elif random_number == 4:
        print('Reply hazy, try.')
        elif random_number == 5:
          print('Ask again later.')
          elif random_number == 6:
            print('Better not tell you now.')
            elif random_number == 7:
              print('My sources say no.')
              elif random_number == 8:
                print('Outlook not so good.')
                else:
                  answer = 'Error'        

I still get the exact same error:

  File "magic8.py", line 10
    elif random_number == 2:
       ^
SyntaxError: invalid syntax

The elifs shouldn’t be indented. This makes it seem that they should somehow be run if random_number == 1.

1 Like

The following should work:

import random
name = "Me"
question = "Can dogs fly?"
answer = " " #what is the use of this variable?
random_number =  random.randint(1, 9)
if random_number == 1:
    print("Yes - definitely")
elif random_number == 2:
    print("It is decidedly so.")
elif random_number == 3:
    print("Without a doubt.")
elif random_number == 4:
    print("Reply hazy, try.") #shouldn't it be "Reply hazy, try again."?
elif random_number == 5:
    print("Ask again later")
elif random_number == 6:
    print("Better not tell you now.")
elif random_number == 7:
    print("My sourcer say no.")
elif random_number == 8:
    print("Outlook not so good.")
else:
    print("Error") #answer = "error" won't work because answer isn't printed after it being given the value
1 Like

This did end up working for indentations. Another place I failed was using print() statements instead of “answer =” like they wanted me to. In general, I should be using down arrow to move to the next line, not enter, right? as enter does the indentation and kept it going. Thank you!

You’re welcome! The down arrow key moves the cursor to the next line but, if there is no next line made yet it won’t work. I suggest enter and any number of backspace keys.