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)
5 ответов
+ 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)
+ 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.
+ 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.
0
Thanks a lot! Not it works however i do not undrtstand the reason it shows different reaults in calculating using different approaches:)
0
Okay, thanks a lot for the clarification!