14. Magic 8 Ball (+15 XP)


# Random


In Python, modules are .py files containing Python code that can be imported inside another Python program. The Python standard library contains well over 200 modules that we can use.

We can use the .randint() function from a module called random to generate a random number from a range.

But first, let’s import this module so we can use its functions.

          
          import random
          
        

Next, we’ll create a variable to store the randomly generated value. Declare a variable called random_number, and assign it to the function call:

          
          random.randint(1, 9)
          
        

This will generate a random number between 1 and 9 (inclusive of both).

Together, the code will look like:

          
          import random

          num = random.randint(1, 9)

          print(num)
          
        

The output should be different each time it runs: 2, 0, 8, 5, 9, 2, 1, 3...



# Instructions


Magic 8 Ball is a popular office toy and children's toy invented in 1940's for fortune-telling and advice seeking. 🎱

It's an oversized 8 ball with some of the following answers:

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

Create a magic8.py program that can answer any Yes or No questions with a different fortune/advice each time it executes.

The output of should have the following format:

          
          Question:      [Question]
          Magic 8 Ball:  [Answer]
          
        

For example:

          
          Question:      Is this a simulation?
          Magic 8 Ball:  Better not tell you now.
          
        

Back

Solution: magic_8_ball.py