Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf465001

Browse files
authored
Merge pull requestrealpython#18 from realpython/clean-wax-poetic-challenge
Clean-up Wax Poetic challenge solution
2 parentsa5d6327 +c6d0c31 commitf465001

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Generate a random poem based on a set structure
66

7-
fromrandomimportchoice
7+
importrandom
88

99
noun= [
1010
"fossil",
@@ -57,54 +57,57 @@
5757
defmake_poem():
5858
"""Create a randomly generated poem, returned as a multi-line string."""
5959
# 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)
6363
# Make sure that all the nouns are different
6464
whilen1==n2:
65-
n2=choice(noun)
65+
n2=random.choice(noun)
6666
whilen1==n3orn2==n3:
67-
n3=choice(noun)
67+
n3=random.choice(noun)
6868

6969
# 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)
7373
whilev1==v2:
74-
v2=choice(verb)
74+
v2=random.choice(verb)
7575
whilev1==v3orv2==v3:
76-
v3=choice(verb)
76+
v3=random.choice(verb)
7777

7878
# 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)
8282
whileadj1==adj2:
83-
adj2=choice(adjective)
83+
adj2=random.choice(adjective)
8484
whileadj1==adj3oradj2==adj3:
85-
adj3=choice(adjective)
85+
adj3=random.choice(adjective)
8686

8787
# Pull two different prepositions
88-
prep1=choice(preposition)
89-
prep2=choice(preposition)
88+
prep1=random.choice(preposition)
89+
prep2=random.choice(preposition)
9090
whileprep1==prep2:
91-
prep2=choice(preposition)
91+
prep2=random.choice(preposition)
9292

9393
# Pull one adverb
94-
adv1=choice(adverb)
94+
adv1=random.choice(adverb)
9595

96-
if"aeiou".find(adj1[0])!=-1:#first letter is a vowel
96+
if"aeiou".find(adj1[0])!=-1:#First letter is a vowel
9797
article="An"
9898
else:
9999
article="A"
100100

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+
)
106108

107109
returnpoem
108110

109111

110-
print(make_poem())
112+
poem=make_poem()
113+
print(poem)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp