+ 1

Ballpark Orders (Python) need help with Test #3-5 failing

I have not finished the courses yet and I'm still a beginner. My code is failing on Test #3-5, not sure exactly why as I cannot see the inputs. Here's my code : order = input() Water = 4.0 Nachos = 6.0 Pizza = 6.0 Cheeseburger = 10.0 Coke = 5.0 price = 0.0 for i in order.split(): if i == "Water" : price += Water elif i == "Nachos" : price += Nachos elif i == "Pizza" : price += Pizza elif i == "Cheeseburger" : price += Cheeseburger elif i == "Coke" : price += Coke else : price += Coke price = price * 1.07 print(price) What am I doing wrong to fail the last 3 test cases? I

21st Mar 2024, 4:51 AM
Karl Breault
Karl Breault - avatar
10 Respostas
+ 1
From a book I read, round() function in Python uses Bankers Rounding. round(47.5) # 48 Case A (left side: 47, right side: 50) round(48.5) # 48 Case B (left side: 48, right side: 50) round(2.15, 1) # 2.2 Case A (left side: 1, right side: 50) round(2.25, 1) # 2.2 Case B (left side: 2, right side: 50) round(2.151, 1) # 2.2 Case A (left side: 1, right side: 51) round(2.251, 1) # 2.3 Case B (left side: 2, right side: 51) A. If the number on the left side of the ndigits is odd, and the right side is 50 or more, it rounds up. B. If the number on the left side of the ndigits is even, and the right side is less than 51, it rounds down.
21st Mar 2024, 10:40 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Karl Breault the code is well done. The problem is not in the logic or the calculation. The problem is the output. Code Coach is expecting a two-digit decimal for cents and it is getting more than two digits after the decimal. Theoretically, we would expect the answer to calculate to only two decimal digits. However, Code Coach answers sometimes happen to fall in a touchy area where binary floating point format cannot precisely store some decimal numbers. [It is like trying to represent the fraction 1/3 in decimal: 0.333333....] Here are two ways to help your code pass the tests. -> Shift the number up by two digits, convert to integer, then shift it back down. print(int(price*100)/100) -> Or, format the output to limit it to 2 decimal places. Here is a way by using an f-string: print(f"{price:.2f}")
21st Mar 2024, 6:16 AM
Brian
Brian - avatar
+ 2
I used round(res, 2) and worked for me.
21st Mar 2024, 6:20 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Karl Breault your approach of separately calculating tax aligns with one of the principles used in professional financial computations. If you are interested in seeing how the problem would be coded overall by using professional fintech principles, see what I put together for you to learn from: https://sololearn.com/compiler-playground/cwCfK2drRR40/?ref=app The code there passes all the tests.🧐 At the extreme opposite, here is a one-liner that also passes all the tests: print(sum({"Water" : 4.28, "Cheeseburger":10.70, "Pizza" : 6.42, "Nachos" : 6.42 }.get(order_item, 5.35) for order_item in input().split() ) ) 😁
21st Mar 2024, 10:56 PM
Brian
Brian - avatar
+ 1
While the task description did not mention, try to round the price to 2 digits.
21st Mar 2024, 5:57 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
all , For reference: Code Coach Ballpark Orders (Easy) https://www.sololearn.com/coach/15?ref=app I wouldn't use round() in a real-life cashier scenario, because it rounds to the nearest even digit. "if two multiples are equally close, rounding is done toward the even choice" -- https://docs.python.org/3/library/functions.html#round print(round(1.5)) # 2 print(round(2.5)) # 2 However, I suspected the Sololearn writers didn't know that and probably used it themselves, so I tried round(total * tax, 2) in my code, and it passed all the test cases, so I left it in.
21st Mar 2024, 10:22 AM
Rain
Rain - avatar
+ 1
It is very difficult to explain how computer handle floating point value. Consider this code: x = 0.1 + 0.1 + 0.1 x == 0.3 # False print(x) # 0.30000000000000004 Computer store value in binary code. If you divide 10 by 3, it will be 0.3333333333 and never ends, but computer has limited memory and cannot store floating point value precisely as the above example. Back to your question, one can only guess the combination of foods ordered plus the tax gives a very slight difference on the decimal with different approach, but the big enough to make it differ from the expected answers.
21st Mar 2024, 12:54 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
So here's what I did while I was in bed trying stuffs and it passed, but I'm still unsure why and what is the difference. I added a variable called : tax = 0.07 In the print function, I used the code : print(price + (price * tax)) I'd like to know why this code passed the test but not my previous ones, I don't really understand the difference. In my mind they are both identically, but I could be wrong.
21st Mar 2024, 12:36 PM
Karl Breault
Karl Breault - avatar
0
Which programing language Will be good for beginners...!
21st Mar 2024, 1:04 PM
Xavier Borgoyary Basumatary
Xavier Borgoyary Basumatary - avatar
0
Xavier Borgoyary Basumatary , BASIC. Not taught here though.
21st Mar 2024, 5:36 PM
Rain
Rain - avatar