Magic 8-Ball Project Isuue

Why is it that whenever I leave a question empty it still displays a name along with an answer? The intended purpose was for it to show "The Magic 8-Ball cannot provide a fortune unless you ask it something. ".

import random
random_number = random.randint(1, 9)

#print(random_number)
name = “DJ”
question = “”
answer = “”

if name == “”:
print("Question: " + question)
else:
print(name + " asks: " + question)
if question == “”:
print("The Magic 8-Ball cannot provide a fortune unless you ask it something. ")
else:
print("Magic 8-Ball’s answer: " + answer)

if random_number == 1:
print(“yes-definitely.” , answer)
elif random_number == 2:
print(“It is decidedly so.” , answer)
elif random_number == 3:
print(“Without a doubt.” , answer)
elif random_number == 4:
print(“Reply hazy, try again.” , answer)
elif random_number == 5:
print(“Ask again later.” , answer)
elif random_number == 6:
print(“Better not tell you now.” , answer)
elif random_number == 7:
print(“My Sources say no.” , answer)
elif random_number == 8:
print(“Outlook not so good.” , answer)
elif random_number == 9:
print(“Very doubtful” , answer)
else:
answer = “Error”

You already defined name variable name = "DJ". When program checking if name == "" it returns false, so else works (it prints name)
After there is one more if if question == "" is True, which prints "The Magic 8-Ball cannot provide a fortune unless you ask it something. "
In the end no matter previous if construction works another if which will print answer every time code is running

1 Like

Thanks! So what would be a better way to structure it in order to get the desired result?