Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKieran Forbes
3,706 PointsNeed help with my Python Code
I have written the code already but are finding problems with the Value Error and the bubble sort.
total = []
prices = []
print("Hi there, and welcome to our store, please enter 5 items you bought today along with their prices")
for loop in range(5):
try:
purchase = input("What did you buy today?")
total.append(purchase)
price = float(input("How much did it cost?"))
prices.append(price)
except ValueError:
print("Please enter a valid number")
def bubbleSort(nlist):
sorted = False
while not sorted:
swaps = 0
for i in range(len(nlist) -1):
if nlist[i] > nlist[i+1]:
temp1 = nlist[i]
nlist[i] = nlist[i + 1]
nlist[i + 1] = temp1
swaps = swaps + 1
if swaps == 0:
sorted = True
return nlist
bubbling = (bubbleSort(prices))
first_total = (total[0], prices[0])
second_total = (total[1], prices[1])
third_total = (total[2], prices[2])
fourth_total = (total[3], prices[3])
fifth_total = (total[4], prices[4])
final_total = first_total, second_total, third_total, fourth_total, fifth_total
print(final_total)
discount = first_total
print("Since you bought 5 products with us today, we will now give you the cheapest product for free, meaning you will get", discount, "for free")
totaltotal =(prices[1], prices[2], prices[3], prices[4])
totaltotalt = sum(totaltotal)
print("So the total price that you will pay today is ",totaltotalt)
Kieran Forbes
3,706 PointsThanks for your reply. The code is working fine and the loop produces the correct values after, but when testing the value error for example by keying in a word instead of a float, the code will run, until the last enret and then an index error would appear. So my ValueError is not working properly.
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsThis issue can be looked at in one of two ways:
- try/except/ValueError does not guarantee that 5 prices and totals are captured, or
- there are hardcoded expectations that 5 valid values are being returned
You may wish to change the for
loop to while len(prices) < 5
:
The hardcoded statements could be make to work with whatever values are present:
first_total = (total[0], prices[0])
final_total = list(zip(total, prices))
print(final_total)
discount = first_total
print("Since you bought", len(total), "products with us today, we will now give you the cheapest product for free, meaning you will get", discount, "for free")
totaltotal = prices[1:]
totaltotalt = sum(totaltotal)
Other issues:
- Since
bubbleSort
operates on a mutable list, the argument will be modified in place. The return valuebubbling
is not necessary and isn't actually used. The sort can simply be called usingbubbleSort(prices)
- If a valid item is entered into
total.append
butprices
fails due toValueError
then the length oftotal
andprices
will be different. Move the assignment oftotal.append
to after thefloat(input(...))
then if the float fails, neither list is appended. - consider using
Decimal
class for money, since0.1 + 0.1 + 0.1 = 0.30000000000000004
due to floating point roundoff errors
Post back if you have more questions. Good luck!!
Kieran Forbes
3,706 PointsThanks for your help, I appreciate it. lll try implement into my program what you mentioned.
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsCan describe the specific problem you are seeing?