Magic 8-Ball-Python-3: Hi! Why is that?

import random

name = “Joe”
question = “Will I win the lottery?”
answer = “”
random_number = random.randint(1, 9)

print(random_number)

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”
else:
answer = “Error”

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

tt

1 Like

I think ? needs quotes as well like this:

print(name + "asks: " + question + “?”)

Also, is the code indented OK? (hard to tell with the copy and pasted code)

2 Likes

Hi
Did you find an answer to that task? I’m stuck on that too. Please help)
(same problem)

1 Like

import random

name = “Joe”

question = “Will I win the lottery?”

answer = “”

random_number = random.randint(1, 9)

print(random_number)

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”

else:

answer = “Error”

print(name + " asks: " + question)

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

1 Like

How to get user input here? Can someone please clarify?
We declare ‘name’ and ‘question’ in the beginning of the code. What should i type to get input from the user. It throws error when i use name= input()
Thanks!

1 Like

How to get user input here? Can someone please clarify?
We declare ‘name’ and ‘question’ in the beginning of the code. What should i type to get input from the user. It throws error when i use name= input()
Thanks!

import random

name = input("Enter your name: ")
question = input("What is your question? ")
#Now you can call 'user' in print statements like so:

print(f"Your name is {name}")
print(f"You asked: {question}")
#another way you might see it printed is like this:
print("Your name is: " + name)
print("You asked: " + question)
#rest of your code below

I’ll also link you the doc so you can review input() from the Codecademy docs:

Python | Built-in Functions | input() | Codecademy

Hope this helps!