forked from asweigart/the-big-book-of-small-python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspongecase.py
53 lines (40 loc) · 1.39 KB
/
spongecase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""sPoNgEcAsE, by Al Sweigart [email protected]
Translates English messages into sPOnGEcAsE.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, word"""
import random
try:
import pyperclip # pyperclip copies text to the clipboard.
except ImportError:
pass # If pyperclip is not installed, do nothing. It's no big deal.
def main():
"""Run the Spongecase program."""
print('''sPoNgEtExT, bY aL sWeIGaRt [email protected]
eNtEr YoUr MeSsAgE:''')
spongecase = englishToSpongecase(input('> '))
print()
print(spongecase)
try:
pyperclip.copy(spongecase)
print('(cOpIed SpOnGeCasE to ClIpbOaRd.)')
except:
pass # Do nothing if pyperclip wasn't installed.
def englishToSpongecase(message):
"""Return the spongecase form of the given string."""
spongecase = ''
useUpper = False
for character in message:
if not character.isalpha():
spongecase += character
continue
if useUpper:
spongecase += character.upper()
else:
spongecase += character.lower()
# Flip the case, 90% of the time.
if random.randint(1, 100) <= 90:
useUpper = not useUpper # Flip the case.
return spongecase
# If this program was run (instead of imported), run the game:
if __name__ == '__main__':
main()