Magic8.py ......Ok, my if statement comes up as a indentationerror: expected an idented block

import random

name = “Brandon”
question = “Is Darth Vador my Father?”
answer = “”

random_number = random.randint(1, 9)

commented out print(random_number)

if random_number == 1:
answer = “Yes - definitely.”
elif random_number == 2:
answer = “It is decidedly so.”
elif random_number == 3:
answer = “Without a doubt.”
elif random_number == 4:
answer = “Reply hazy, try again.”
elif random_number == 5:
answer = “Ask again later.”
elif random_number == 6:
answer = “Better not tell you now.”
elif random_number == 7:
answer = “My sources say no.”
elif random_number == 8:
answer = “Outlook not so good.”
elif random_number == 9:
answer = “Very doubtful.”
else:
answer = “Error”
print(name + " asks: " + question)
print(“Magic 8-Ball’s answer:” + answer)

If you can still edit, follow the directions in this topic to format your code sample so we can see what you see in the editor.

3 Likes

You are using no spaces instead of four spaces. Try doing the following (I’m just showing a part of your code)

if random_number == 1:
    answer = "Yes - Definetly."

And thus, add four spaces with If-else statement contents


name = "Brandon"
question = "Is Darth Vader my Father?"
answer = ""

random_number = random.randint(1, 9)

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

That is so full of errors it might be best to take another crack at it.

1 Like

I hear ya, but it’s exactly the same as the example solution.

Done, and Done, had to fix all the spacing of the elif statements, and the answer statements, but it runs now!!!

1 Like

Way to go. A lesson learned is hard to erase, especially with practice. You never forget how hard you worked to debug code. Those lessons tend to stick better than when someone just tells you about it.

2 Likes

Thank you Sir, I had been getting that same Error during lesson exercises, but never knew what it was. My lesson was complete and I could go to the next lesson, but that Error would pop up. Glad I know now. :sunglasses:

1 Like