The print
statements which print out the result come before the other print statements. That means they will print first.
You’re referring to this exercise:
Magic 8-Ball
Reply hazy, try again.
Jose asks: Will I get another dog named Naruto?
Magic 8-Ball’s answer:
The reason you’re getting this is because the random_number = 4. Then, it prints “Reply hazy, try again.” After that, the code gets out of the if-elif-else statement and nothing will be printed!
There’re a few issues in your code:
-
The variable answer is only assigned to “Error” in the else statement. But, this else statement will not happen because we force the random_number to be 1 to 9 and these have been taken care in the if-elif statements.
random_number = random.randint(1,9)
-
Instead of printing out the answer in each of the if-elif statement, you should be assigning to the variable answer.
-
The last 2 lines of codes should not be indented. You have aligned it with answer = “Error”. This means they will only be executed when the else condition is true.
else:
answer = "Error"
print(name + " asks; " + question)
print("Magic 8-Ball's answer:" + answer)
import random
loopname = True
name = input("Welkom bij de Magische 8-Ball! Voer uw naam in om te beginnen: ")
answer = “”
while loopname:
random_number = random.randint(1, 11)
if random_number == 1:
answer = “Ja, absoluut.”
elif random_number == 2:
answer = “Het is een gegeven dat het zo is.”
elif random_number == 3:
answer = “Zonder twijfel.”
elif random_number == 4:
answer = “Vraag het zo nog eens.”
elif random_number == 5:
answer = “Waarom vraag je dit?”
elif random_number == 6:
answer = “Dat kan ik je nu beter niet vertellen.”
elif random_number == 7:
answer = “Mijn bronnen zeggen nee.”
elif random_number == 8:
answer = “Het ziet er naar uit van niet.”
elif random_number == 9:
answer = “Ik heb vele twijfels.”
elif random_number == 10:
answer = “Ik wil nu even niet praten.”
elif random_number == 11:
answer = “Ga gewoon weg!”
else:
answer = ‘Error’
question = input("Stel de Magische 8-Ball een vraag: ")
if question == “”:
print(“Antwoord van de Magische 8-Ball: You’ve got to ask me something before I can answer”)
elif question == “Doei”:
print(“Antwoord van de Magische 8-Ball: Daag sterveling!”)
break
else:
if name == “”:
print(“Vraag:”,question)
else:
print(name,“vraagt:”,question)
print(“Antwoord van de Magische 8-Ball:”,answer)
This is written in a while loop, so if you run it in .py it will keep on asking you for input, answers and such a written in dutch but you can easily replace them.
This is a community forums associated with a learning environment, simply posting answers or solution code is frowned upon as mentioned in the Community Guidelines
Hi guys, first time on here. Just wanted to share my 8 ball code, don’t imagine its particularly amazing but I’m proud of it, my first piece of code that actually dose something lol. I wrote it as a function, I did try and get it to take inputs from he user, on there name and question, but ran into issues with that.
name = ‘Adam’
question = ‘Is the answer 42?’
answer = ‘’
import random
rand_num =random.randint(1,10)
def ansbot(rand_num):
if rand_num == 1:
return('Yes - definitely.')
elif rand_num ==2:
return('It is decidedly so.')
elif rand_num ==3:
return('Without a doubt.')
elif rand_num ==4:
return('Reply hazy,try again.')
elif rand_num ==5:
return('Ask again later.')
elif rand_num ==6:
return('Better not tell you now.')
elif rand_num ==7:
return('My sources say no.')
elif rand_num ==8:
return('Outlook not so good.')
elif rand_num ==9:
return('Very doubtful.')
elif rand_num ==10:
return('KABOOM')
else:
return('Error')
if question == ‘’:
print(‘You realise you have to ask a question right?’)
elif name == ‘’:
print(str(question))
print('Magic 8-Balls)'s answer: ’ + str(ansbot(rand_num)))
else:
print(str(name)+ ’ asks: ’ + str(question))
print('Magic 8-Balls)'s answer: ’ + str(ansbot(rand_num)))
In order to take input from a user you have to use the input()
function. For example.
def ansbot(rand_num):
name = input('Enter your name: ')
question = input('Enter your Question: ')
You’re running into issues from that else return 'Error'
portion; git rid of it. It’s blocking the rest of the code from executing. Also, on two lines of code you need to use a backslash to escape the quotation marks:
print('Magic 8-Balls)\'s answer: ' + str(ansbot(rand_num)))
I tried that, but i get an EOF error, heres what it looks like ona differen tbit of code ive written
Traceback (most recent call last):
File “shipping.py”, line 1, in
weight = input('Hi there, welcome to Sals Shipping caculator. Ill help you find the best way to ship your product, I just need the weight. How much does your package weight in pounds?: ')
EOFError: EOF when reading a line
this is for the following code:
weight = input('Hi there, welcome to Sals Shipping caculator. Ill help you find the best way to ship your product, I just need the weight. How much does your package weight in pounds?: ')
Codecademy is not an ideal platform to carry out experiments, one of the biggest problem is that not all the lessons support input()
method, I would run your code on another website (like this one), that should work
I see, i didnt realise that. Thanks so much for the pointer!
This is what I came up with. I closed off whitespace and if the question variable is empty we output a print message. Also, I set a default parameter for empty name variables. We would have our users be assigned a Guest with an int from 100 - 100k.
Importing the random module to use its modules
import random
Variable name will hold the value of the user’s name.
name = "Jaime ".strip()
.join a space because with .strip() we remove the whitespace in name.
name = " ".join(name.split())
Our question variable will hold the value of our user’s question.
Same thing as for the variable name. We strip the whitespace and join a space after removing the whitespace.
question = “Will it rain?”.strip()
We join a single space after removing the whitespace.
question = " ".join(question.split())
Our teller’s answer variable.
answer = “”
random_number to store our generated answer.
random_number = random.randint(1,10)
Testing our random function
It was a success! print(random_number)
#reference for our conditional statements:
“”"
Yes - definitely.
It is decidedly so.
Without a doubt.
Reply hazy, try again.
Ask again later.
Better not tell you now.
My sources say no.
Outlook not so good.
Very doubtful.
“”"
Our answers conditional statements.
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 = “The odds are 50-50.”
else:
answer = “Error”
Reference for our condtional statement:
“”"
print(final_name, “asks:”, question + “?”)
print(“Magic 8-Ball’s answer:”, answer)
“”"
Our conditional statements:
if question == “” or ‘’:
print(“Must type in your question before proceeding.”)
elif name == “” or ‘’:
print(“Guest” + " " + str(random.randint(100,100000)) + " " + “asks:”, question)
print("")
(print(“Magic 8-Ball’s answer:”, answer))
elif type(name) == str:
print(name, “asks:”, question + “?”)
print(“Magic 8-Ball’s answer:”, answer)
The errors in are happening because you need to change the print() in the if elif statements to answer =. This was my solution to that problem.
import random
name = ‘’
question = ‘Do it be thanging’
answer = ‘’
random_number = random.randint(1,9)
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’
if question == ‘’:
print(‘You are playing with time and space itself’)
elif name == ‘’:
print(‘Question: ’ + question)
else:
print(name + ’ asks: ’ + question)
if question == ‘’:
print(‘You must ask a question to recieve an answer’)
else:
print(’'‘Magic 8-Ball’s answer: ‘’’ + answer)
name = “Lily”
question = “Will I be famous and rich?”
answer = “”
import random
random_number = random.randint(1, 10)
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 = “Signs point to yes”
else:
answer = “Error”
print(name + " asks: " + question)
print("Magic 8-Ball’s answer: " + answer)
Hello everyone,
these are my first couple of times here on Codecademy forums.
Regarding the Magic 8 ball exercise, this is what I have for my input
name = “”
question = “Will I in my lifetime ever see aliens coming to Earth?”
answer = “”
import random
random_number = random.randint(1,10)
print(random_number)
if name == “”:
print(“Question:” + question)
else:
print(name+ " asks:“+ question)
if random_number == 1:
answer = “Yes - definiteily”
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 = “Don’t count on it”
else:
answer = “Error”
print(name + " asks:” + question)
print(“Magic 8-Ball’s answer:” + answer)
my out put is this
1
Question:Will I in my lifetime ever see aliens coming to Earth?
asks:Will I in my lifetime ever see aliens coming to Earth?
Magic 8-Ball’s answer:Yes - definiteily
what I am trying to figure out is why the terminal being redundant and outputting “Question:” and " asks:" instead of just “Question” when I don’t intput a name?
Most likely, the second to last line of your code is executed every time you run it. The only question would be regarding indentation, but given your output, I’d say that is what is happening. For future posts please see How do I format code in my posts? Preserving your code’s original format is extremely helpful especially in Python where indentation determines scope.
Thank you @midlinder for your support!
I actually found the mistake.
I added a redundant phrase somewhere.
Now it works perfectly.
Hi, I’ve been experimenting with the Magic 8-Ball section and I’d like to know how to get the name and then ’ asks: ’ to appear when using random.choice (I found that online, not part of the course). Right now, it will output “asks: Bethany” and I can’t figure out how to reverse that order, “Bethany asks:”
Thank you.
Here is the code:
#Assigning the name, question and answer
Name = ''
Question = ''
Answer = ''
name_list = ['Bethany', 'Kevin', 'Shakwandaleesha']
question_list = ['Will I be any good at coding?', 'Do I smell?', 'Should I play the lottery?']
#importing the random module
import random
#Name List
print(random.choice(name_list))
#Question List
print(random.choice(question_list))
#Randomising the number
random_number = random.randint(1, 10)
#M8 Ball answer
print('🎱 Magic 8-Ball Says: '+ Answer)
#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')
else:
print('Error ❌')```
I am writing the code of magic ball- 8 in python .
Please someone help me in resolving the problem;
Error:
File “magic8.py”, line 9
elif random_number == 2:
^
SyntaxError: invalid syntax
import random
name = “Sam”
question = “Will I ever get to see Sam again?”
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)
We cannot tell because the posted code has lost its original formatting. Be sure that each line below an if
or elif
is indented.
Sir , I’m a novice in the field of coding. I’ll be grateful if you can elaborate it bit more. Thank you.
Just as if
, while
, for
have code blocks similar to def
and class
, they also require the indentation syntax to isolate the block.
if random_number == 1:
answer = "Yes - definitely"
Not sure this will raise a syntax error if the indentation is missing. It might be more like, ‘expected indent’.
In the new user section of the forum is a description of how to post code samples such that they preserve their original formatting. Please take a minute to track that down and repost your code in a new reply.