+ 2

Why it prints 14.445 instead of 14.45 when I input 3 ?

x = int(input()) n = 5 n *= x if x > 1 : for i in range(x) : z = ((n * 10) / 100) e = n - z y = ((e * 7) / 100) print(y + e) elif x == 1 : y = ((n * 7) / 100) print(y + n) I have a project and that is, You sell items that cost 5 Dollars and if the customer buys more than 1, he will get a 10% discound on all of them. And the tax is 7% (doesnt matter how many he buys). For example : Input: 4 Output: 19.26 Explanation: Buys 4 items and gets 10% discound on all of them and the 7% tax, the total is 19.26. But the thing is that this code outputs 14.445 when I input 3, it should output 14.45. And I need help! I dont know why it doesnt work when I input 3. Any help will be appreciated.

11th Dec 2022, 12:09 PM
Engineer X
Engineer X - avatar
5 Answers
0
Thank you everyone, your answers really help me to find a solution. I was looking for this: x = int(input()) n = 5 n *= x if x > 1 : for i in range(x) : x1 = ((n * 10) / 100) x2 = n - x1 x3 = ((x2 * 7) / 100) x4 = x3 + x2 print(round(x4, 2)) elif x == 1 : y = ((n * 7) / 100) y1 = n + y print(y1) Now instead of 14.445 it prints 14.45 when I input 3.
17th Dec 2022, 6:30 AM
Engineer X
Engineer X - avatar
+ 7
the code is trying to calculate the sum of a 7% tax on n and the value of n itself. However, it is using floating point arithmetic to perform the calculations, which can lead to inaccuracies in the final result due to rounding errors. This is likely why the result is slightly different than expected. To fix this issue, you could try using the round() function to round the final result to the nearest integer. This would ensure that the result is more accurate and matches the expected value more closely. https://code.sololearn.com/cIT9q8KQhd4A/?ref=app
12th Dec 2022, 2:31 PM
Sadaam Linux
Sadaam Linux - avatar
+ 4
# try for example to round the results to only two decimals: round(aResult,2)
11th Dec 2022, 12:33 PM
JaScript
JaScript - avatar
+ 1
Hi JaScript , Thanks for the reply but you explain a little about it? Because I have never used "round".
11th Dec 2022, 3:02 PM
Engineer X
Engineer X - avatar
+ 1
The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer. For example: x = round(5.7432, 2) print(x) Will print 5.72 because the number given after the comma will specify number of decimals. x=round(5.7453) print(x) will print 6 If the firsr decimal is greater than 5 it will print the next number or it will print same number Hope you understand
12th Dec 2022, 1:27 PM
Kishore Kumar