Ask a question, get a mysterious answer!
Build a Magic 8-Ball that gives random answers to your questions using if/else, comparisons, and randomness!
Time for your second project! A Magic 8-Ball is a toy that gives random answers to yes-or-no questions. You will build one using variables, input, if/else, and a random number generator. Ask it anything!
๐ก Think of it like this:
The real Magic 8-Ball is a plastic ball with a 20-sided die floating in liquid. You shake it, and one of 20 answers floats to the top. Your digital version will do the same thing โ but with code!
This project brings together decisions, randomness, and user input. It shows you how to build something interactive that responds differently every time.
Where you see this in real life:
The original Magic 8-Ball was invented in 1950 and has 20 possible answers: 10 positive, 5 negative, and 5 non-committal!
A Magic 8-Ball gives random answers to yes-or-no questions. You will build one using the randommodule, if/elif/else, and input().
First, we need to import the random module. Then we can userandom.randint(a, b)to get a random number between a and b.
Run it multiple times โ you get different results!We will pick a random number from 1 to 5. Each number will correspond to a different answer.
Click โถ Run to see the output!Now use if/elif/else to turn each number into a fun answer!
Click โถ Run to see the output!Try adding more answers! Change randint(1, 5) to randint(1, 8) and add more elif branches.
Click โถ Run to see the output!What does random.randint(1, 6) do?