Magic Fortune Ball

Tags: tiny, beginner, humour

The Magic Fortune Ball can predict the future and answer your yes/no questions with 100 percent accuracy using the power of Python’s random number module. This program is similar to a Magic 8 Ball toy, except you don’t have to shake it. It also features a function for slowly printing text strings with spaces in between each character, giving the messages a spooky, mysterious effect.

Most of the code is dedicated to setting the eerie atmosphere. The program itself simply selects a message to display in response to a random number.

magic_fortune_ball.py
 1"""Magic Fortune Ball, by Al Sweigart al@inventwithpython.com
 2Ask a yes/no question about your future. Inspired by the Magic 8 Ball.
 3This code is available at https://nostarch.com/big-book-small-python-programming
 4Tags: tiny, beginner, humor"""
 5
 6import random, time
 7
 8
 9def slowSpacePrint(text, interval=0.1):
10    """Slowly display text with spaces in between each letter and
11    lowercase letter i's."""
12    for character in text:
13        if character == 'I':
14            # I's are displayed in lowercase for style:
15            print('i ', end='', flush=True)
16        else:
17            # All other characters are displayed normally:
18            print(character + ' ', end='', flush=True)
19        time.sleep(interval)
20    print()  # Print two newlines at the end.
21    print()
22
23
24# Prompt for a question:
25slowSpacePrint('MAGIC FORTUNE BALL, BY AL SWEiGART')
26time.sleep(0.5)
27slowSpacePrint('ASK ME YOUR YES/NO QUESTION.')
28input('> ')
29
30# Display a brief reply:
31replies = [
32    'LET ME THINK ON THIS...',
33    'AN INTERESTING QUESTION...',
34    'HMMM... ARE YOU SURE YOU WANT TO KNOW..?',
35    'DO YOU THINK SOME THINGS ARE BEST LEFT UNKNOWN..?',
36    'I MIGHT TELL YOU, BUT YOU MIGHT NOT LIKE THE ANSWER...',
37    'YES... NO... MAYBE... I WILL THINK ON IT...',
38    'AND WHAT WILL YOU DO WHEN YOU KNOW THE ANSWER? WE SHALL SEE...',
39    'I SHALL CONSULT MY VISIONS...',
40    'YOU MAY WANT TO SIT DOWN FOR THIS...',
41]
42slowSpacePrint(random.choice(replies))
43
44# Dramatic pause:
45slowSpacePrint('.' * random.randint(4, 12), 0.7)
46
47# Give the answer:
48slowSpacePrint('I HAVE AN ANSWER...', 0.2)
49time.sleep(1)
50answers = [
51    'YES, FOR SURE',
52    'MY ANSWER IS NO',
53    'ASK ME LATER',
54    'I AM PROGRAMMED TO SAY YES',
55    'THE STARS SAY YES, BUT I SAY NO',
56    'I DUNNO MAYBE',
57    'FOCUS AND ASK ONCE MORE',
58    'DOUBTFUL, VERY DOUBTFUL',
59    'AFFIRMATIVE',
60    'YES, THOUGH YOU MAY NOT LIKE IT',
61    'NO, BUT YOU MAY WISH IT WAS SO',
62]
63slowSpacePrint(random.choice(answers), 0.05)

https://inventwithpython.com/bigbookpython/project42.html