0

Ballpark orders

Hey! Can you please help me with the advice - what’s wrong with my code in the challenge Ballpark orders? It passes 1 and 2 tests but not the 3,4,5. Thank you in advance! zakaz = input () zak = zakaz.split() cost = 0 for i in zak: if i == "Pizza": cost += 6.00 elif i == "Nachos": cost += 6.00 elif i == "Cheeseburger": cost += 10.00 elif i == "Water": cost += 4.00 elif i == "Coke": cost += 5.00 else: cost += 5.00 total = cost * 1.07 print (total)

5th Jun 2023, 9:15 PM
Alexander Glyantsev
5 Answers
+ 2
Hi, Please check the difference in the output of these two commands to find out what is wrong: x = 10 print(x * 1.07) print(x + x * 0.07)
5th Jun 2023, 10:34 PM
Artin Azari
Artin Azari - avatar
+ 2
Bad news after some experimenting: it's a rounding error. Python doesn't make that easy to fix by just using a native long double or decimal type instead of regular double, unfortunately. One ugly solution is to do tax = cost*7, total = cost + tax/100 ; otherwise you'll need to import something with better precision.
5th Jun 2023, 10:31 PM
Orin Cook
Orin Cook - avatar
+ 2
It's different because binary representations of decimals are a little bit weird. The classic example is 1.01+2.02; try running print(1.01+2.02) and you'll see what I mean. As for the underlying reason it's weird, that's because of the way binary floating point numbers are stored: in two parts, let's say a and b, where the number being stored is equal to a * 2^b (plus an extra bit for the sign). This can cause small rounding errors sometimes.
6th Jun 2023, 7:10 PM
Orin Cook
Orin Cook - avatar
0
Thanks a lot! Not it works however i do not undrtstand the reason it shows different reaults in calculating using different approaches:)
6th Jun 2023, 6:36 PM
Alexander Glyantsev
0
Okay, thanks a lot for the clarification!
7th Jun 2023, 1:21 PM
Alexander Glyantsev