Python Magic 8-Ball

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?

1 Like

Welcome to the forums.

Please post your code so we can see what you’ve already written. What works, what doesn’t?

1 Like

Yah l think if l see in code what you are saying, then l will be able to offer help.

1 Like
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?

Well, write it out and try different things to see what happens. You’ve already written a good deal of code here.

Also, 13 won’t be a randomly generated number b/c of how you’ve set up random.randint()

1 Like

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(“Question is not expected!” # I just give an example answer here.

Hope to answer your questions

1 Like