Python magic 8 ball

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi Everyone,
I am on learn python 3 Magic 8-Ball section of the course.

Whenever I run my code it rearranges the order of the print statement in a manner that doesn’t make sense. Here is my code,

import random

name = "Jose"

question = "Will I get another dog named Naruto?"

answer = ""

random_number = random.randint(1,9)


if random_number == 1:

  print("Yes - definitely.")

elif random_number == 2:

  print("It is decidely 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.")


  answer = "Error"


  print(name + " asks; " + question)

  print("Magic 8-Ball's answer:" + answer)

Is there anything I could be doing better?

Thank you advance.
Learn Python 3 | Codecademy


Someone will help you soon, but first they’ll need to be able to read your code :slight_smile: . With python formatting is not only easier to read, it’s crucial to how the program runs. Please look over this link for instructions on how to format your code: [How to] Format code in posts

1 Like

What do you mean by this? Every time you run the program, a random number is generated which changes the outcome

these lines:

  print(name + " asks; " + question)

  print("Magic 8-Ball's answer:" + answer)

are part of your else clause/body, is that desired?

1 Like

Yes, the second portion
print(name + " asks; " + question)
print(“Magic 8-Ball’s answer:” + answer)
It wouldn’t print as the course suggested. ex
It would show up as
name asks: (instead of the question showing up next to it it goes under it)

Because you nested these prints in your else clause. Like I asked, is that right/desired?

This is the output i get after I ran the code above.

Reply hazy, try again.
Jose asks: Will I get another dog named Naruto?
Magic 8-Ball’s answer:

And once I look at someone who did it correctly they had their print statements in the bottom of all of the else/elif statements.
So idk what I doing wrong

The print statements which print out the result come before the other print statements. That means they will print first.

You’re referring to this exercise:
Magic 8-Ball

Reply hazy, try again.
Jose asks: Will I get another dog named Naruto?
Magic 8-Ball’s answer:

The reason you’re getting this is because the random_number = 4. Then, it prints “Reply hazy, try again.” After that, the code gets out of the if-elif-else statement and nothing will be printed!

There’re a few issues in your code:

  1. The variable answer is only assigned to “Error” in the else statement. But, this else statement will not happen because we force the random_number to be 1 to 9 and these have been taken care in the if-elif statements. random_number = random.randint(1,9)

  2. Instead of printing out the answer in each of the if-elif statement, you should be assigning to the variable answer.

  3. The last 2 lines of codes should not be indented. You have aligned it with answer = “Error”. This means they will only be executed when the else condition is true.


  answer = "Error"


  print(name + " asks; " + question)

  print("Magic 8-Ball's answer:" + answer)