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?

https://www.codecademy.com/courses/learn-python-3/projects/python-magic-8-ball

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()

https://docs.python.org/3/library/random.html

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(“[PLEASE ENTER PASSCODE]”)
else:
print(“Question is not expected!” # I just give an example answer here.

Hope to answer your questions

1 Like

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.

https://gist.github.com/codecademydev/3f29ff2ea2537c38ff7219adcc0b32f5

Check the indentation of your else statement. It should match the indentation level of your elif statements.

1 Like

you were right, thank you so much. That error was driving me crazy, even more so at the fact that I couldn’t see the problem.

1 Like