Magic 8-Ball

For the Magic 8-Ball Project, How do I add the #14 task: The question string?

import random

name = “Batman”

question = “Will I catch Joker today?”

answer = “”

random_number = random.randint(1, 13)
print(random_number)

if random_number == 1:
answer = “Yes - definately”
elif random_number == 2:
answer = “It is decidedly so”
elif random_number == 3:
answer = “Wihtout 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 tel 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”
elif random_number == 10:
answer = “Yup!”
elif random_number == 11:
answer = “uh-huh”
elif random_number == 12:
answer = “Nope!”
elif random_number == 13:
answer = “nuh-huh”
else:
answer = “Error”

if name == “”:
print("Question: " + question)
else:
print(name, “asks:”, question)
print(“Magic 8-Ball’s answer:\n” + answer)

Please follow this going forward:

It’s difficult to read your code b/c it’s not formatted.

That said:

  • why do the random numbers go to 13? (I thought it was 10(?). Unless I did a different/outdated lesson).
  • If I’m understanding your question correctly, there are a couple things you have to fix:
    – the logic for what to do if no name is provided needs tweaking. Hint: use len()
    –there’s a typo in the else part.
    –the logic for not asking a question would be similar to what you’d write if no name was provided. (minus the else portion).

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.