Magic 8-Ball

Hi everyone! I’m a bit stuck with this exercise. Whenever I run the code, I get ‘Error’ for ‘Magic 8-Ball’s Answer’ and not any of the randomly generated answers. I’ve looked at the code carefully and am not quite sure where I’m going wrong. Any help would be greatly appreciated!

Here is my code:

import random

name = "Rachael"

question = "Will it rain on Monday?"

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)

Here is the link to the exercise: Magic 8-Ball.

Thanks in advance!

Hey and welcome to the forums! :wave:

Double check the values you are using for comparisons inside the if statement compared the the actual value of random_number. The values in the if statements are all strings, does this match what random_number actually is?

Hope that helps! :smile:

Thank you notlyall, that sorted it! Funny how you can miss little things like that when you’ve been looking at the same block of code for a while :sweat_smile:

1 Like

Haha yeah tell me about it, it’s the little bugs that can be the most annoying sometimes!

Glad I could help :slight_smile:

1 Like