Magic 8-Ball Python3 Project | Nested IF-ELSE Statements

Hello,
I’m trying to follow the guidelines of the project for the Python3 course with their Magic 8-Ball program. Their problem statement wants us to provide a conditional output based on the presence/absence of parameters. My solution was to use indents to create a nested IF-ELSE statement, but it doesn’t seem to work.

import random

name = "Richard"
question = "Will I enjoy dinner tonight?"
answer = ""

random_number = random.randint(1,10)

if question == "":
  "Please ask a question first."
else:
  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."
  elif random_number == 10:
    answer = "Why do you ask such a      question?"
  else:
    answer = "Error"

if name == " ":
  print("Question: " + question)
else:
  print(name + " asks: " + question)

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

I copied your code and it ran it. It runs.

What’s the issue?

1 Like

Thanks for the response.

The instructions ask for an alternative handling when there is no question value (empty string) for the question variable. Instead of saying:

Your question: (blank)
The answer: (random answer)

Say:
Where’s your question?

It runs, but I can’t get the nested if-else statement to work. Other lessons say that you can potentially do this by indenting code, but it hasn’t worked that way.

What I wanted to achieve:

if (condition)
do this
else
do this block (
block of conditions
)

Let me offer a hint:

if question == "":
  print("Please ask a question first.")
elif random_number == 1:
  answer = "Yes - definitely."
elif random_number == 2:
  answer = "It is decidedly so."