Magic 8-Ball help :(

Hey all! (Please be gentle with me, I’m on day two of learning :3)

I’m stuck on the Magic 8-Ball project - I think I don’t fully understand how it works. I understand how if/elif/else work in theory but I’m stuck on actually running it practically

I’ve inputted the code exactly as it said I should:

import random

name = "Dave"
question = "Will I learn Python?"
answer = ""

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

if random_number == 1:
  print("Yes - definitely!")
elif random_number == 2:
  print("It is decidedly so")
elif random_number ==3:
  print("without 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 now..")
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")
  print("404 error")

print(name + " asks: " + question)
print("Magic 8-ball's answer: " + answer) 

When I print I’m seeing it like this:

without a doubt.

Dave asks: Will I learn Python?
Magic 8-ball's answer: 

The answer is coming before and I’m not sure what I’m doing wrong? I’ve checked the solution that is isted and it looks exactly like mine (clearly not - but I don’t know what I’m missing.)

What I don’t understand is how it would print the answer correctly like this in the first place when the answer variable is an empty string - I understand why it prints it blank based on that logic. I’ve tried a few different things but I either get a syntax error or a type error from where I’ve tried to amend the value of the answer variable.

Any help would be greatly appreciated! :slight_smile:

Double check the instructions for lesson 6. What are you supposed to be doing with the new string you create?

The order here is entirely linear so if one statement appears in your code before another it will be executed before it too.

This is why I’m stuck - because it prints in a linear way, I kind of understand why it’s showing up as empty in that final section when I’m trying to run the program with the name of the person, their question and then the answer with the randomly generated numbers - because I’ve defined the “answer” variable as an empty string to begin with. So the answer I’m getting is kind of what I assumed I would get, but it’s not what the instructions or the solution code implies I should get.

I’m confused by two things - why the randomly generated number is being executed prior to the question or request for answer - especially since I commented out the test print of that. Second is how I would have to phrase the print() function in order to get the random_number variable included within the response.

:pleading_face: I’m not sure if I’m making much sense - I’ve been following the guide exactly, but I’m guessing I haven’t fully understood a core concept somewhere down the line.

Task 6 says this- " answer is assigned to the phrase “Yes - definitely.”". There’s a big difference between printing a value and assigning it. I’ve not looked at the solution code but I would assume it expects, and uses, assignment, a = b.

If not then that will always cause trouble. As you say, the answer references an empty string at the start and that is never changed with only print statements so you’re right on that part, but be aware that print is designed just for output.

1 Like

… I feel very stupid. Thank you for your help - I went back over the lesson and also over what I was doing. I figured out what I was doing wrong. (And I’ve also just seen your second response confirming this as I type!)

In the if/elif/else statements I was printing the value instead of equating the answer to it, which 100% makes sense.

Thank you so much!

1 Like

I’d not worry about it much just so long as you remember it. Sometimes the bits of code where you really stumble and have to question yourself are the ones you remember best. Whenever functions are introduced remember the same thing though, that print is designed just for text output (another bit where folks can trip up).

1 Like

you know i find this option of code easier and more helpful -
import random
randnum = random.randint(1,8)
answer = “”
print(“I am the magic 8 ball”)
print(“You must have heard about me…(I tell prophecies)”)
name = input(“So what’s your name?”)
print(“Jolly good! Hey there, “+name+”. Come let’s get going!”)
ask8ball = input(“What do you want to ask me “+name+”? P.s. it should be about something in the distant future.”)
if randnum == 1:
answer = “Yes - definitely.”
elif randnum ==2:
answer=(“Without a doubt.”)
elif randnum == 3:
answer =(“hmm! My Reply is quite hazy, try again”)
elif randnum == 4:
answer=("Ask again later plz “)
elif randnum == 5:
answer=(“Better not tell you know”)
elif randnum == 6:
answer=(“My sources say no”)
elif randnum == 7:
answer=(“Very doubtful”)
elif randnum == 8:
answer=(“Maybe not…sorry :|”)
print(name +” asked 8 ball "+ask8ball)
print("8 ball gave the answer: "+answer)
print(“THAT’S MAGIC!!”)

try this one it’s like a more advanced but good version of it!