Skip to content

Commit acd8380

Browse files
committed
Cleanup
1 parent b4dfc60 commit acd8380

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

ch17-graphical-user-interfaces/6-challenge.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def check_unique(word_list):
10-
"""check that all items in a list are unique and return True or False"""
10+
"""Check that all items in a list are unique and return True or False"""
1111
unique_words = []
1212
for word in word_list:
1313
if word not in unique_words:
@@ -16,7 +16,7 @@ def check_unique(word_list):
1616

1717

1818
def make_poem():
19-
"""create a randomly generated poem, returned as a multi-line string"""
19+
"""Create a randomly generated poem, returned as a multi-line string"""
2020

2121
# split the user input into lists
2222
noun = entry_noun.get().split(",")
@@ -36,7 +36,7 @@ def make_poem():
3636
result_poem.config(text="Please do not enter duplicate words.")
3737
return
3838

39-
# make sure that we got enough words from the user to make a poem
39+
# Make sure that we got enough words from the user to make a poem
4040
if (
4141
len(noun) < 3
4242
or len(verb) < 3
@@ -51,20 +51,20 @@ def make_poem():
5151
)
5252
return
5353

54-
# otherwise, we can go ahead with generating a poem:
54+
# Otherwise, we can go ahead with generating a poem:
5555

56-
# pull three nouns randomly
56+
# Pull three nouns randomly
5757
n1 = random.choice(noun)
5858
n2 = random.choice(noun)
5959
n3 = random.choice(noun)
6060

61-
# make sure that all the nouns are different
61+
# Make sure that all the nouns are different
6262
while n1 == n2:
6363
n2 = random.choice(noun)
6464
while n1 == n3 or n2 == n3:
6565
n3 = random.choice(noun)
6666

67-
# pull three different verbs
67+
# Pull three different verbs
6868
v1 = random.choice(verb)
6969
v2 = random.choice(verb)
7070
v3 = random.choice(verb)
@@ -73,7 +73,7 @@ def make_poem():
7373
while v1 == v3 or v2 == v3:
7474
v3 = random.choice(verb)
7575

76-
# pull three different adjectives
76+
# Pull three different adjectives
7777
adj1 = random.choice(adjective)
7878
adj2 = random.choice(adjective)
7979
adj3 = random.choice(adjective)
@@ -82,37 +82,38 @@ def make_poem():
8282
while n1 == n3 or n2 == n3:
8383
adj3 = random.choice(adjective)
8484

85-
# pull two different prepositions
85+
# Pull two different prepositions
8686
prep1 = random.choice(preposition)
8787
prep2 = random.choice(preposition)
8888
while prep1 == prep2:
8989
prep2 = random.choice(preposition)
9090

91-
# pull one adverb
91+
# Pull one adverb
9292
adv1 = random.choice(adverb)
9393

9494
if "aeiou".find(adj1[0]) != -1: # first letter is a vowel
9595
article = "An"
9696
else:
9797
article = "A"
9898

99-
# put it all together into a poem
99+
# Put it all together into a poem
100100
poem = f"{article} {adj1} {n1}\n\n"
101101
poem = poem + f"{article} {adj1} {n1} {v1} {prep1} the {adj2} {n2}\n"
102102
poem = poem + f"{adv1}, the {n1} {v2}\n"
103103
poem = poem + f"the {n2} {v3} {prep2} a {adj3} {n3}"
104104

105-
# place the resulting poem into the label
105+
# Place the resulting poem into the label
106106
result_poem.config(text=poem)
107107

108108

109-
# create the application window and add a Frame
109+
# Create the application window and add a Frame
110110
window = tk.Tk()
111111
window.title("Make your own poem!")
112112
frame = tk.Frame()
113-
frame.grid(padx=5, pady=5) # pad top and left of frame 5 pixels before grid
113+
# Pad top and left of frame 5 pixels before grid
114+
frame.grid(padx=5, pady=5)
114115

115-
# create and add text labels for text entry boxes
116+
# Create and add text labels for text entry boxes
116117
label_directions = tk.Label(
117118
frame, text="Enter your favorite words, separated by commas:"
118119
)
@@ -128,7 +129,7 @@ def make_poem():
128129
label_adv = tk.Label(frame, text="Adverbs:")
129130
label_adv.grid(row=6, column=1, sticky=tk.S + tk.E)
130131

131-
# create and add space for user entry of text
132+
# Create and add space for user entry of text
132133
entry_noun = tk.Entry(frame, width=80)
133134
entry_noun.grid(row=2, column=2)
134135
entry_verb = tk.Entry(frame, width=80)
@@ -140,30 +141,31 @@ def make_poem():
140141
entry_adv = tk.Entry(frame, width=80)
141142
entry_adv.grid(row=6, column=2)
142143

143-
# create and add 'Create poem' button
144+
# Create and add 'Create poem' button
144145
btn_create = tk.Button(frame, text="Create your poem", command=make_poem)
145146
btn_create.grid(row=7, column=1, columnspan=2)
146147

147-
# create label into which to output the poem
148+
# Create label into which to output the poem
148149
result_poem = tk.Label(frame)
149150
result_poem.grid(row=9, column=1, rowspan=5, columnspan=2)
150151

151152

152-
# function to save the poem displayed in the output box into a text file
153153
def save_file():
154+
"""Save the poem displayed in the output box into a text file"""
154155
type_list = [("Text files", "*.txt")]
155156
file_name = filedialog.asksaveasfilename(
156157
filetypes=type_list, defaultextension="*.txt"
157158
)
158-
print(file_name)
159-
if file_name != "": # save file if user entered a file name
159+
# Save file if user entered a file name
160+
if file_name != "":
160161
output_file = open(file_name, "w")
161162
output_file.writelines(result_poem.cget("text"))
162163
output_file.close()
163164

164165

165-
# create and add 'Save your poem' button
166+
# Create and add 'Save your poem' button
166167
btn_save = tk.Button(frame, text="Save your poem", command=save_file)
167168
btn_save.grid(row=15, column=1, columnspan=2)
168169

169-
window.mainloop() # start the application
170+
# Start the application
171+
window.mainloop()

0 commit comments

Comments
 (0)