|
54 | 54 | ]
|
55 | 55 |
|
56 | 56 |
|
57 |
| -def make_poem(): |
58 |
| - """Create a randomly generated poem, returned as a multi-line string.""" |
59 |
| - # Pull three nouns randomly |
60 |
| - n1 = random.choice(noun) |
61 |
| - n2 = random.choice(noun) |
62 |
| - n3 = random.choice(noun) |
63 |
| - # Make sure that all the nouns are different |
64 |
| - while n1 == n2: |
65 |
| - n2 = random.choice(noun) |
66 |
| - while n1 == n3 or n2 == n3: |
67 |
| - n3 = random.choice(noun) |
| 57 | +def give_me_some(lst, num): |
| 58 | + """return a list made of num element of a given list, ensuring there are no duplicates""" |
| 59 | + result = [] |
68 | 60 |
|
69 |
| - # Pull three different verbs |
70 |
| - v1 = random.choice(verb) |
71 |
| - v2 = random.choice(verb) |
72 |
| - v3 = random.choice(verb) |
73 |
| - while v1 == v2: |
74 |
| - v2 = random.choice(verb) |
75 |
| - while v1 == v3 or v2 == v3: |
76 |
| - v3 = random.choice(verb) |
| 61 | + for i in range(num): |
| 62 | + picked = random.choice(lst) |
| 63 | + # We ensure that this element was not picked already |
| 64 | + while picked in result: |
| 65 | + picked = random.choice(lst) |
| 66 | + result.append(picked) |
| 67 | + return result |
77 | 68 |
|
78 |
| - # Pull three different adjectives |
79 |
| - adj1 = random.choice(adjective) |
80 |
| - adj2 = random.choice(adjective) |
81 |
| - adj3 = random.choice(adjective) |
82 |
| - while adj1 == adj2: |
83 |
| - adj2 = random.choice(adjective) |
84 |
| - while adj1 == adj3 or adj2 == adj3: |
85 |
| - adj3 = random.choice(adjective) |
86 | 69 |
|
87 |
| - # Pull two different prepositions |
88 |
| - prep1 = random.choice(preposition) |
89 |
| - prep2 = random.choice(preposition) |
90 |
| - while prep1 == prep2: |
91 |
| - prep2 = random.choice(preposition) |
| 70 | +def right_article(adj): |
| 71 | + """Depending of the first letter of a given article, send back article a or an""" |
| 72 | + vowel = ("a", "e", "i", "o", "u") |
| 73 | + if adj[0] in vowel: |
| 74 | + return "an" |
| 75 | + else: |
| 76 | + return "a" |
92 | 77 |
|
93 |
| - # Pull one adverb |
94 |
| - adv1 = random.choice(adverb) |
95 | 78 |
|
96 |
| - if "aeiou".find(adj1[0]) != -1: # First letter is a vowel |
97 |
| - article = "An" |
98 |
| - else: |
99 |
| - article = "A" |
| 79 | +def make_poem(): |
| 80 | + """Create a randomly generated poem, returned as a multi-line string.""" |
| 81 | + # Pull nouns, verbs, adj, prep and adv as requested |
| 82 | + nouns = give_me_some(noun, 3) |
| 83 | + verbs = give_me_some(verb, 3) |
| 84 | + adjectives = give_me_some(adjective, 3) |
| 85 | + prepositions = give_me_some(preposition, 2) |
| 86 | + adv = give_me_some(adverb, 1) |
100 | 87 |
|
| 88 | + # Prepare the right articles |
| 89 | + first_article = right_article(adjectives[0]) |
| 90 | + second_article = right_article(adjectives[2]) |
| 91 | + |
101 | 92 | # Create the poem
|
102 | 93 | 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 |
| - ) |
| 94 | + f"{first_article.capitalize()} {adjectives[0]} {nouns[0]}" |
| 95 | + f"\n\n{first_article.capitalize()} {adjectives[0]} {nouns[0]} {verbs[0]} {prepositions[0]} the {adjectives[1]} " |
| 96 | + f"{nouns[1]}\n{adv[0]}, the {nouns[0]} {verbs[1]}" |
| 97 | + f"\nthe {nouns[1]} {verbs[2]} {prepositions[1]} {second_article} {adjectives[2]} {nouns[2]}" |
| 98 | + ) |
108 | 99 |
|
109 | 100 | return poem
|
110 | 101 |
|
|
0 commit comments