|
4 | 4 |
|
5 | 5 | # Generate a random poem based on a set structure |
6 | 6 |
|
7 | | -from random import choice |
| 7 | +import random |
8 | 8 |
|
9 | 9 | noun = [ |
10 | 10 | "fossil", |
|
57 | 57 | def make_poem(): |
58 | 58 | """Create a randomly generated poem, returned as a multi-line string.""" |
59 | 59 | # Pull three nouns randomly |
60 | | - n1 = choice(noun) |
61 | | - n2 = choice(noun) |
62 | | - n3 = choice(noun) |
| 60 | + n1 = random.choice(noun) |
| 61 | + n2 = random.choice(noun) |
| 62 | + n3 = random.choice(noun) |
63 | 63 | # Make sure that all the nouns are different |
64 | 64 | while n1 == n2: |
65 | | - n2 = choice(noun) |
| 65 | + n2 = random.choice(noun) |
66 | 66 | while n1 == n3 or n2 == n3: |
67 | | - n3 = choice(noun) |
| 67 | + n3 = random.choice(noun) |
68 | 68 |
|
69 | 69 | # Pull three different verbs |
70 | | - v1 = choice(verb) |
71 | | - v2 = choice(verb) |
72 | | - v3 = choice(verb) |
| 70 | + v1 = random.choice(verb) |
| 71 | + v2 = random.choice(verb) |
| 72 | + v3 = random.choice(verb) |
73 | 73 | while v1 == v2: |
74 | | - v2 = choice(verb) |
| 74 | + v2 = random.choice(verb) |
75 | 75 | while v1 == v3 or v2 == v3: |
76 | | - v3 = choice(verb) |
| 76 | + v3 = random.choice(verb) |
77 | 77 |
|
78 | 78 | # Pull three different adjectives |
79 | | - adj1 = choice(adjective) |
80 | | - adj2 = choice(adjective) |
81 | | - adj3 = choice(adjective) |
| 79 | + adj1 = random.choice(adjective) |
| 80 | + adj2 = random.choice(adjective) |
| 81 | + adj3 = random.choice(adjective) |
82 | 82 | while adj1 == adj2: |
83 | | - adj2 = choice(adjective) |
| 83 | + adj2 = random.choice(adjective) |
84 | 84 | while adj1 == adj3 or adj2 == adj3: |
85 | | - adj3 = choice(adjective) |
| 85 | + adj3 = random.choice(adjective) |
86 | 86 |
|
87 | 87 | # Pull two different prepositions |
88 | | - prep1 = choice(preposition) |
89 | | - prep2 = choice(preposition) |
| 88 | + prep1 = random.choice(preposition) |
| 89 | + prep2 = random.choice(preposition) |
90 | 90 | while prep1 == prep2: |
91 | | - prep2 = choice(preposition) |
| 91 | + prep2 = random.choice(preposition) |
92 | 92 |
|
93 | 93 | # Pull one adverb |
94 | | - adv1 = choice(adverb) |
| 94 | + adv1 = random.choice(adverb) |
95 | 95 |
|
96 | | - if "aeiou".find(adj1[0]) != -1: # first letter is a vowel |
| 96 | + if "aeiou".find(adj1[0]) != -1: # First letter is a vowel |
97 | 97 | article = "An" |
98 | 98 | else: |
99 | 99 | article = "A" |
100 | 100 |
|
101 | | - # add lines to poem |
102 | | - poem = f"{article} {adj1} {n1}\n\n" |
103 | | - poem = poem + f"{article} {adj1} {n1} {v1} {prep1} the {adj2} {n2}\n" |
104 | | - poem = poem + f"{adv1}, the {n1} {v2}\n" |
105 | | - poem = poem + f"the {n2} {v3} {prep2} a {adj3} {n3}" |
| 101 | + # Create the poem |
| 102 | + poem = ( |
| 103 | + f"{article} {adj1} {n1}\n\n" |
| 104 | + f"{article} {adj1} {n1} {v1} {prep1} the {adj2} {n2}\n" |
| 105 | + f"{adv1}, the {n1} {v2}\n" |
| 106 | + f"the {n2} {v3} {prep2} a {adj3} {n3}" |
| 107 | + ) |
106 | 108 |
|
107 | 109 | return poem |
108 | 110 |
|
109 | 111 |
|
110 | | -print(make_poem()) |
| 112 | +poem = make_poem() |
| 113 | +print(poem) |
0 commit comments