Magic 8-ball projects

#imported numbers for magic 8-ball
import random
random_number = random.randint(1,12)

#variables defined - name, question asked and answer
name = “Ariel”
question = “Will I get chocolate today?”
answer = “”

#random number generated by system

print(random_number)

#conditionals that tell program what to say based on generated 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.”
elif random_number == 10:
answer = " The shun is shinning on you today!"
elif random_number == 11:
answer = “Your sister ate it!”
elif random_number == 12:
answer = “Sure!”
else:
answer = “Error”

if name == “”:
print(name + " asks: " + question)
else:
print(name + " asks: " + question)

if question == “”:
print(“Please enter your question.”)
else:
print("Magic 8 - ball’s answer: " + answer)

This is one of the first projects I have successful made and I wanted to share it with you guys. If you have any pointers on improving it I welcome it. :slight_smile:

Hello!

Well done on completing the project! In future please use this guide on how to format code when sharing it as it just makes things a little clearer, especially for languages such as Python that are so indentation sensitive.

One little bit of feedback I would have on the project itself if you don’t mind is this conditional:

if name == "":
  print(name + " asks: " + question)
else:
  print(name + " asks: " + question)

Since the same thing is executed regardless of the conditional, there isn’t strictly a need to have the conditional at all, and you could just have a single print statement instead of the whole if/else block.

Happy coding! :smile: