I’m currently working on the Python Magic-8 Ball project. How would I make an answer that only shows up when a certain question is asked? And if that question is not asked but the random number is the one assigned to the answer to that question, how would I give it a different answer?
import random
name = "Fleur"
question = "Am I a robot?"
answer = ""
random_number = random.randint(1,12)
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 = "Be patient."
elif random_number==11:
answer = "Please leave me alone."
elif random_number==12:
answer = "¯\_(ツ)_/¯"
else:
answer = "Error."
if question=="" and name=="":
print("Please ask a question.")
elif question=="":
print("Please ask a question.")
elif name=="":
print("Question: " + question)
print("Magic 8-Ball's answer: "+ answer)
else:
print(name, " asks: ", question)
print("Magic 8-Ball's answer: "+ answer)
Here is my current code. I want to make the number 13 have the answer “42.”, but only if the question asked is “What is the secret to life, the universe, and everything?”. If that question is not asked and the number generated is still 13, I want the answer to be “[PLEASE ENTER PASSCODE]”. How would I go about this?
Hi space-flowers,
I read your code. I think you had very good questions. Thank you for that! There are some ways to do it. One way is below.
For your number 13, first, you should change to random_number = random.randint(1, 13) to get a possible random_number output of 13. Then, in the first if…elif block, you should add case 13 with answer = “42”. Next, in the second if…elif block, you add a specical elif for your number 13, for example:
elif random_number == 13:
if question == “What is the secret to life, the universe, and everything?”:
print(name, " asks: ", question)
print("Magic 8-Ball’s answer: "+ answer)
elif question == “”:
print(“[PLEASE ENTER PASSCODE]”)
else:
print(“Question is not expected!” # I just give an example answer here.
Hi, can someone look at my code for the magic 8-ball project? I keep getting a syntax error message regarding the part about adding an else statement with the "answer = “Error”. Im unable to see the outcome of the code because of this.