Funky code

I was doing the magic 8 ball exercise, and for some reason, after I completed the code, and ran it. It did what it was suppost to do but out of order. I cant figure out why. Here is the code. It always yields the prediction before the question and I cant figure out why. The code seems to be line for line with the solution, but it yields slightly different results .

import random

name = “Joe”
question = (“will I win the lottery?”)
answer = “”

random_number = random.randint(1,9)
#print(random_number)

if random_number == 1:
print("Yes -definetly ")
elif random_number == 2:
print(“It is definetly so”)
elif random_number ==3:
print(“with out a doubt”)
elif random_number == 4:
print(“Reply hazy, try again”)
elif random_number == 5:
print(“Ask again later”)
elif random_number == 6:
print(“Better not tell you know”)
elif random_number == 7:
print (“My sources say no”)
elif random_number == 8:
print (“Outlook not so good”)
elif random_number == 9:
print(“Very doubtful”)
else:
answer = “Error”

print(name + " asks: " + question)
print("Magic 8 Ball’s answer: " + answer)

Might check what your if/elifs are currently doing :slightly_smiling_face:
Do you need to be printing in each one? Or possibly doing something with answer?

2 Likes

Hey there! I am not a pro by any means, but here are some things I noticed.

First when you declare “question”, you use unnecessary parenthesis around the question “Will I win the lottery?”

Also, in my code, I made the answer equal the phrase. Right now in your code, the answer either equals nothing or error.

Hope this helps!

1 Like

I’m so bad at this lol. yes you are correct i should have use answer in stead of print. That fixed the problem

I used print command instead of answer command. Such a silly error. Its always silly errors with me lol.

To be honest, it’s almost always silly errors when you’re starting out. When you get around to using a code editor, the editor will likely be smart enough to mark things like typos and simple errors for you. And if you start using TypeScript, then the TypeScript compiler will mark all kinds of errors for you.

thanks. I have a lot to learn.