(close)

Use this link to get 70% off the Automate the Boring Stuff online video course.

Support me on Patreon

Prev: Simple Substitution Cipher | Next: Sliding Tile Puzzle
Use this link to get 70% off the Automate the Boring Stuff online video course.

Support me on Patreon

Sine Message

This program displays a message of the user’s choice in a wavy pattern as the text scrolls up. It accomplishes this effect with `math.sin()`

, which implements the trigonometric sine wave function. But even if you don’t understand the math, this program is rather short and easy to copy.

When you run *sinemessage.py*, the output will look like this:

```
Sine Message, by Al Sweigart [email protected]
(Press Ctrl-C to quit.)
What message do you want to display? (Max 39 chars.)
> I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
I <3 Programming!
````--snip--`

The `math.sin()`

function in Python’s `math`

module takes an argument, which we’ll call *x*, and returns another number called the *sine of x*. Several mathematical applications use the sine function; in our program, its purpose is merely to create a neat wave effect. We pass a variable named `step`

to `math.sin()`

. This variable starts at `0`

and increases by `0.25`

on each iteration of the main program loop.

We’ll use the return value of `math.sin()`

to figure out how many spaces of padding we should print on either side of the user’s message. Since `math.sin()`

returns a floating point number between `-1.0`

and `1.0`

, but the minimum amount of padding we want is zero, not a negative value, line 31 adds `1`

to the return value of `math.sin()`

, making the effective range `0.0`

to `2.0`

. We’ll certainly need more than zero to two spaces, so line 31 multiplies this number by a variable named `multiplier`

to increase the amount of padding. The product of this multiplication is the number of spaces of padding to add to the left side before printing the user’s message.

The result is the waving message animation you see when you run the program.

```
1. """Sine Message, by Al Sweigart [email protected]
2. Create a sine-wavy message.
3. View this code at https://nostarch.com/big-book-small-python-projects
4. Tags: tiny, artistic"""
5.
6. import math, shutil, sys, time
7.
8. # Get the size of the terminal window:
9. WIDTH, HEIGHT = shutil.get_terminal_size()
10. # We can't print to the last column on Windows without it adding a
11. # newline automatically, so reduce the width by one:
12. WIDTH -= 1
13.
14. print('Sine Message, by Al Sweigart [email protected]')
15. print('(Press Ctrl-C to quit.)')
16. print()
17. print('What message do you want to display? (Max', WIDTH // 2, 'chars.)')
18. while True:
19. message = input('> ')
20. if 1 <= len(message) <= (WIDTH // 2):
21. break
22. print('Message must be 1 to', WIDTH // 2, 'characters long.')
23.
24.
25. step = 0.0 # The "step" determines how far into the sine wave we are.
26. # Sine goes from -1.0 to 1.0, so we need to change it by a multiplier:
27. multiplier = (WIDTH - len(message)) / 2
28. try:
29. while True: # Main program loop.
30. sinOfStep = math.sin(step)
31. padding = ' ' * int((sinOfStep + 1) * multiplier)
32. print(padding + message)
33. time.sleep(0.1)
34. step += 0.25 # (!) Try changing this to 0.1 or 0.5.
35. except KeyboardInterrupt:
36. sys.exit() # When Ctrl-C is pressed, end the program.
```

After entering the source code and running it a few times, try making experimental changes to it. The comments marked with `(!)`

have suggestions for small changes you can make.

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
`math.sin(step)`

on line 30 to`math.cos(step)`

? - What happens if you change
`math.sin(step)`

on line 30 to`math.sin(0)`

?