Skip to content

Commit 6ecee26

Browse files
authored
Update 5-challenge-wax-poetic.py
I'm learning Python with your great book, but I've few knowledge of some other langages (programming is just a hobby for me). I made this solution, that I find really more Pythonistic, and easier to read. I shared it just in case it may help you to continue update solutions. Keep up the good work !
1 parent 6aa39b8 commit 6ecee26

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

ch09-lists-tuples-and-dictionaries/5-challenge-wax-poetic.py

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,57 +54,48 @@
5454
]
5555

5656

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 = []
6860

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
7768

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)
8669

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"
9277

93-
# Pull one adverb
94-
adv1 = random.choice(adverb)
9578

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)
10087

88+
# Prepare the right articles
89+
first_article = right_article(adjectives[0])
90+
second_article = right_article(adjectives[2])
91+
10192
# Create the poem
10293
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+
)
10899

109100
return poem
110101

0 commit comments

Comments
 (0)