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?
Welcome to the forums.
Please post your code so we can see what you’ve already written. What works, what doesn’t?
Yah l think if l see in code what you are saying, then l will be able to offer help.
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
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]”)
print(“Question is not expected!” # I just give an example answer here.
Hope to answer your questions