Python magic 8 ball

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi Everyone,
I am on learn python 3 Magic 8-Ball section of the course.

Whenever I run my code it rearranges the order of the print statement in a manner that doesn’t make sense. Here is my code,

import random

name = "Jose"

question = "Will I get another dog named Naruto?"

answer = ""

random_number = random.randint(1,9)

#print(random_number)

if random_number == 1:

  print("Yes - definitely.")

elif random_number == 2:

  print("It is decidely 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:

  answer = "Error"

  

  print(name + " asks; " + question)

  print("Magic 8-Ball's answer:" + answer)

Is there anything I could be doing better?

Thank you advance.
LINK
Learn Python 3 | Codecademy

Hi,

Someone will help you soon, but first they’ll need to be able to read your code :slight_smile: . With python formatting is not only easier to read, it’s crucial to how the program runs. Please look over this link for instructions on how to format your code: [How to] Format code in posts

1 Like

What do you mean by this? Every time you run the program, a random number is generated which changes the outcome

these lines:

  print(name + " asks; " + question)

  print("Magic 8-Ball's answer:" + answer)

are part of your else clause/body, is that desired?

1 Like

Yes, the second portion
print(name + " asks; " + question)
print(“Magic 8-Ball’s answer:” + answer)
It wouldn’t print as the course suggested. ex
It would show up as
name asks: (instead of the question showing up next to it it goes under it)
question

Because you nested these prints in your else clause. Like I asked, is that right/desired?

This is the output i get after I ran the code above.

Reply hazy, try again.
Jose asks: Will I get another dog named Naruto?
Magic 8-Ball’s answer:

And once I look at someone who did it correctly they had their print statements in the bottom of all of the else/elif statements.
So idk what I doing wrong

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:

  1. 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)

  2. Instead of printing out the answer in each of the if-elif statement, you should be assigning to the variable answer.

  3. 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

1 Like

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 :slight_smile:

3 Likes

I see, i didnt realise that. Thanks so much for the pointer!