Skip to content

Commit a0547a1

Browse files
committed
Improve code, comments, trailing whitespace
1 parent 1678a5c commit a0547a1

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

Model Code/lesson_2.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ def guessQuality(guess, theNumber):
130130

131131
if guess > theNumber:
132132
print("Too high!")
133-
guessQuality(guess, theNumber)
134133
elif guess < theNumber:
135134
print("Too low!")
136-
guessQuality(guess, theNumber)
137135
else:
138136
print("That's right! Well done!")
139137
break
140138

139+
guessQuality(guess, theNumber) # Both too high and too low use this, so it can go after the if
140+
141141
guessesTaken += 1
142142

143143
if guessesTaken == guessesAllowed:
@@ -169,7 +169,7 @@ def guessQuality(guess, theNumber):
169169
# if modulo is zero, that number is a factor of the other. e.g.: 4 % 2 = 0
170170
print("\n" + str(number) + " is not a prime number.")
171171

172-
numStr = str(number)
172+
numStr = str(number) # Not strictly necessary code but more readable
173173
iterStr = str(x)
174174
pairedFactor = str(number//x)
175175

@@ -178,8 +178,9 @@ def guessQuality(guess, theNumber):
178178
# An extension would be to get all factors of the number!
179179
break
180180
x += 1
181-
else: # Make sure this else is indented to the same level as the 'loop' line!
182-
# What happens if it's indented to match the if statement following that line?
181+
else: # This 'else' is part of the while loop! While/else statements are valid in python
182+
# This statement will only trigger if the condition of the loop is false.
183+
# So when you `break` the loop, it doesn't activate. Here, that's perfect!
183184
print(str(number) + " is a prime number!")
184185

185186
else:
@@ -201,7 +202,7 @@ def guessQuality(guess, theNumber):
201202

202203
turns_remaining = 10 # You need at least as many turns as unique letters in your word!
203204

204-
# We'll count our turns downwards
205+
# We'll count our turns downwards just for a change of pace
205206
while turns_remaining > 0: # while we have turns left
206207

207208
word_display = '' # An empty string we will use to display our incomplete word
@@ -219,7 +220,7 @@ def guessQuality(guess, theNumber):
219220

220221
elif guess in guesses: # To prevent duplicate guesses
221222
print("You already guessed this letter! So far, you have guessed: " + guesses)
222-
223+
223224
else: # If the guess isn't the whole word, previously guessed, and is one character, the program continues.
224225
guesses += guess # Add guess to the string of guesses
225226

Model Code/lesson_3.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ def guessQuality(guess, theNumber):
4343
guess = int(input("What's your guess? \n"))
4444
guessTotal = guessMathsFunction(guess, guessTotal) # A running total of guesses
4545
guessesTaken += 1
46-
46+
4747
if guess > theNumber:
4848
print("Too high!")
49-
guessQuality(guess, theNumber)
5049
elif guess < theNumber:
5150
print("Too low!")
52-
guessQuality(guess, theNumber)
5351
else:
5452
print("That's right! Well done!")
5553
print("Your average guess was " + str(guessTotal / guessesTaken))
5654
break
5755

56+
guessQuality(guess, theNumber)
57+
5858
if guessesTaken == guessesAllowed:
5959
print("Game over! The number was " + str(theNumber))
6060
print("Your average guess was " + str(guessTotal / guessesTaken))
@@ -118,14 +118,32 @@ def guessQuality(guess, theNumber):
118118

119119
print(productInfo)
120120

121-
# Extension
121+
# Extension -- two methods
122+
sortedByPrice = {}
123+
124+
# First method, for each price, check if it exists already in the keys
125+
# If it does, add the product to that list. If it doesn't, make a list with that product in it.
126+
for i in range(len(fruityPrices)):
127+
price = fruityPrices[i]
128+
product = fruityProducts[i] # More readable if you do it like this
129+
130+
if price in sortedByPrice.keys():
131+
sortedByPrice[price].append(product)
132+
else:
133+
sortedByPrice[price] = [product]
134+
135+
print("Method one:", sortedByPrice)
136+
137+
# Method two: get unique prices, compare all prices to unique price, find products matching, group
122138

123139
sortedByPrice = {}
124-
uniquePrices = set(fruityPrices)
125-
# You could do this by iterating over the fruityPrices and removing prices you've seen before, but a set is faster!
140+
141+
uniquePrices = set(fruityPrices)
142+
# You could get uniques by iterating over the fruityPrices and removing prices you've seen before
143+
# but a set is faster!
126144
# That might look like this:
127-
# uniquePrices = []
128-
# for price in fruityPrices:
145+
# uniquePrices = []
146+
# for price in fruityPrices:
129147
# if price not in uniquePrices:
130148
# uniquePrices.append(price)
131149

@@ -137,5 +155,4 @@ def guessQuality(guess, theNumber):
137155

138156
sortedByPrice[price] = byPrice # the list is our group of products
139157

140-
print(sortedByPrice)
141-
# In short: get unique prices, compare all prices to each unique price, find products matching that price and group them
158+
print("Method two:", sortedByPrice)

0 commit comments

Comments
 (0)