Help with the Magic 8 ball exercise

Hello everyone!
My first time posting here so I apologize in advance if it’s in the wrong place.

I am currently working through the python 3 courses and I’m on the Magic 8 ball section.
I have gone through it too many times and When I click save/run nothing happens :frowning: Any advice on what I can do to make it work properly.

import random

name = “Ross”

question = “Why isn’t this working?”

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)

Thanks in advance all

paste your code here like this below with three back quotation marks at the start and end, as here the indentation cannot be seen

triple back quotations

code/program

triple back quotations

///

import random

name = “Ross”
question = “Why isn’t this working?”
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)

///

Hey Woodall132,

I formatted your code and added some comments:

import random

name = "Ross"
question = "Why isn't this working?"
answer = ""
# You were missing an equal sign here.
# You need to set 'random_number'  equal to random.randint(1, 9).
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"

# Indent our print statements to the left.
print(name + " asks: " + question)  
print("Magic 8-Ball's answer " + answer)

So, set ‘random_number’ equal to random.randint(1, 9), indent your print statements to the left, and you should be good - Hope this helps!

1 Like

Ahhhh thank you very much! I’ve been racking my brain yet it was such a simple error. Thanks

Sometimes when debugging it’s also helpful to read your code from the bottom, up. It gives your brain a different perspective.

More on indentation in Python:
https://www.geeksforgeeks.org/indentation-in-python/

1 Like