-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.random.words.py
executable file
·47 lines (33 loc) · 963 Bytes
/
09.random.words.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
#!/usr/bin/env python3
# Type: exercise/project
# Teaches: standard-library, command-line-arguments, file-IO, context-manager
"""
For the next exercise '10-hangman_cli.py' you will need a list of random words generated by this program. For example:
$ ./10-hangman_cli.py "$(./09-random.words.py 5)"
Let's play hangman! 10 guesses left
Hangman says: _____ __________ _______ ___ ____
Guess:
"""
import argparse
import random
parser = argparse.ArgumentParser(description="Random selection of words")
parser.add_argument\
( "count"
, type=int
, help="number of words"
)
args = parser.parse_args()
with open("words.list.txt", "r") as f:
content = f.read()
words = set()
for word in content.split():
if not word.isascii():
continue
if not word.isalpha():
continue
if len(word) <= 1:
continue
words.add(word)
words = list(words)
sample = random.sample(words, args.count)
print(" ".join(sample))