diff --git a/ch09-lists-tuples-and-dictionaries/5-challenge-wax-poetic.py b/ch09-lists-tuples-and-dictionaries/5-challenge-wax-poetic.py index b6384a8..7b9ed8a 100644 --- a/ch09-lists-tuples-and-dictionaries/5-challenge-wax-poetic.py +++ b/ch09-lists-tuples-and-dictionaries/5-challenge-wax-poetic.py @@ -4,7 +4,7 @@ # Generate a random poem based on a set structure -from random import choice +import random noun = [ "fossil", @@ -57,54 +57,57 @@ def make_poem(): """Create a randomly generated poem, returned as a multi-line string.""" # Pull three nouns randomly - n1 = choice(noun) - n2 = choice(noun) - n3 = choice(noun) + n1 = random.choice(noun) + n2 = random.choice(noun) + n3 = random.choice(noun) # Make sure that all the nouns are different while n1 == n2: - n2 = choice(noun) + n2 = random.choice(noun) while n1 == n3 or n2 == n3: - n3 = choice(noun) + n3 = random.choice(noun) # Pull three different verbs - v1 = choice(verb) - v2 = choice(verb) - v3 = choice(verb) + v1 = random.choice(verb) + v2 = random.choice(verb) + v3 = random.choice(verb) while v1 == v2: - v2 = choice(verb) + v2 = random.choice(verb) while v1 == v3 or v2 == v3: - v3 = choice(verb) + v3 = random.choice(verb) # Pull three different adjectives - adj1 = choice(adjective) - adj2 = choice(adjective) - adj3 = choice(adjective) + adj1 = random.choice(adjective) + adj2 = random.choice(adjective) + adj3 = random.choice(adjective) while adj1 == adj2: - adj2 = choice(adjective) + adj2 = random.choice(adjective) while adj1 == adj3 or adj2 == adj3: - adj3 = choice(adjective) + adj3 = random.choice(adjective) # Pull two different prepositions - prep1 = choice(preposition) - prep2 = choice(preposition) + prep1 = random.choice(preposition) + prep2 = random.choice(preposition) while prep1 == prep2: - prep2 = choice(preposition) + prep2 = random.choice(preposition) # Pull one adverb - adv1 = choice(adverb) + adv1 = random.choice(adverb) - if "aeiou".find(adj1[0]) != -1: # first letter is a vowel + if "aeiou".find(adj1[0]) != -1: # First letter is a vowel article = "An" else: article = "A" - # add lines to poem - poem = f"{article} {adj1} {n1}\n\n" - poem = poem + f"{article} {adj1} {n1} {v1} {prep1} the {adj2} {n2}\n" - poem = poem + f"{adv1}, the {n1} {v2}\n" - poem = poem + f"the {n2} {v3} {prep2} a {adj3} {n3}" + # Create the poem + poem = ( + f"{article} {adj1} {n1}\n\n" + f"{article} {adj1} {n1} {v1} {prep1} the {adj2} {n2}\n" + f"{adv1}, the {n1} {v2}\n" + f"the {n2} {v3} {prep2} a {adj3} {n3}" + ) return poem -print(make_poem()) +poem = make_poem() +print(poem)