Some strange test case for y*(1-x) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Some strange test case for y*(1-x)

This pice of code cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 print(sum(cart)-sum(cart)*discount/100) provide different output in comparison with this one cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 print(sum(cart)*(1-discount/100)) On some hidden test. Can you suggest what is the hidden test case consists of?

10th Apr 2023, 7:25 PM
Иван Поздняков
Иван Поздняков - avatar
2 Answers
+ 8
Иван Поздняков , > since the differences in result depends on how the discount is calculated, most exercises define how the final result should be rounded, but not in this case. > the exercise is in *python fof beginners* exercise 32.2. > the task description gives a clear hint *how* the calculation has to be done : > Use a for loop to iterate over the list. > Use the following formula to calculate the result of X% discount on $Y price: Y - (Y*X/100). (my comment: this formula has to be applied on each item in the cart.)
10th Apr 2023, 7:57 PM
Lothar
Lothar - avatar
+ 5
Floating point numbers differ in precision values... If one result 0.45 then other may result 0.44789889 ... Better you go with first approach.
10th Apr 2023, 7:37 PM
Jayakrishna 🇮🇳