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.
The Program in Action
When you run magicfortuneball.py, the output will look like this:
M A G i C F O R T U N E B A L L , B Y A L S W E i G A R T
A S K M E Y O U R Y E S / N O Q U E S T i O N .
> Isn't fortune telling just a scam to trick money out of gullible people?
L E T M E T H i N K O N T H i S . . .
. . . . . . . .
i H A V E A N A N S W E R . . .
A F F i R M A T i V E
How It Works
The only thing the Magic Fortune Ball actually does is display a randomly chosen string. It completely ignores the user’s question. Sure, line 28 calls input('> '), but it doesn’t store the return value in any variable because the program doesn’t actually use this text. Letting users enter their questions gives them the sense that the program has an aura of clairvoyance.
The slowSpacePrint() function displays the uppercase text with any letter I in lowercase, making the message look unique. The function also inserts spaces between each character of the string and then displays them slowly, with pauses. A program doesn’t need to be sophisticated enough to predict the future to be fun!
1. """Magic Fortune Ball, by Al Sweigart [email protected]
2. Ask a yes/no question about your future. Inspired by the Magic 8 Ball.
3. View this code at https://nostarch.com/big-book-small-python-projects
4. Tags: tiny, beginner, humor"""
6. import random, time
9. def 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)
17. # All other characters are displayed normally:
18. print(character + ' ', end='', flush=True)
20. print() # Print two newlines at the end.
24. # Prompt for a question:
25. slowSpacePrint('MAGIC FORTUNE BALL, BY AL SWEiGART')
27. slowSpacePrint('ASK ME YOUR YES/NO QUESTION.')
28. input('> ')
30. # Display a brief reply:
31. replies = [
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...',
44. # Dramatic pause:
45. slowSpacePrint('.' * random.randint(4, 12), 0.7)
47. # Give the answer:
48. slowSpacePrint('I HAVE AN ANSWER...', 0.2)
50. answers = [
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',
60. 'YES, THOUGH YOU MAY NOT LIKE IT',
61. 'NO, BUT YOU MAY WISH IT WAS SO',
63. slowSpacePrint(random.choice(answers), 0.05)
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
Check that the player’s question ends in a question mark.
Add other answers the program can give.
Exploring the Program
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
What happens if you change random.randint(4, 12) on line 45 to random.randint(4, 9999)?
What error do you get if you change time.sleep(1) on line 49 to time.sleep(-1)?