@@ -43,18 +43,18 @@ def guessQuality(guess, theNumber):
43
43
guess = int (input ("What's your guess? \n " ))
44
44
guessTotal = guessMathsFunction (guess , guessTotal ) # A running total of guesses
45
45
guessesTaken += 1
46
-
46
+
47
47
if guess > theNumber :
48
48
print ("Too high!" )
49
- guessQuality (guess , theNumber )
50
49
elif guess < theNumber :
51
50
print ("Too low!" )
52
- guessQuality (guess , theNumber )
53
51
else :
54
52
print ("That's right! Well done!" )
55
53
print ("Your average guess was " + str (guessTotal / guessesTaken ))
56
54
break
57
55
56
+ guessQuality (guess , theNumber )
57
+
58
58
if guessesTaken == guessesAllowed :
59
59
print ("Game over! The number was " + str (theNumber ))
60
60
print ("Your average guess was " + str (guessTotal / guessesTaken ))
@@ -118,14 +118,32 @@ def guessQuality(guess, theNumber):
118
118
119
119
print (productInfo )
120
120
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
122
138
123
139
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!
126
144
# That might look like this:
127
- # uniquePrices = []
128
- # for price in fruityPrices:
145
+ # uniquePrices = []
146
+ # for price in fruityPrices:
129
147
# if price not in uniquePrices:
130
148
# uniquePrices.append(price)
131
149
@@ -137,5 +155,4 @@ def guessQuality(guess, theNumber):
137
155
138
156
sortedByPrice [price ] = byPrice # the list is our group of products
139
157
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