0

Why my code doesn't meet all the testcases ??

Here is the problem: You are getting ready to paint a piece of art. The canvas and brushes that you want to use will cost 40.00. Each color of paint that you buy is an additional 5.00. Determine how much money you will need based on the number of colors that you want to buy if tax at this store is 10%. Task Given the total number of colors of paint that you need, calculate and output the total cost of your project rounded up to the nearest whole number. Input Format An integer that represents the number of colors that you want to purchase for your project. Output Format A number that represents the cost of your purchase rounded up to the nearest whole number. Sample Input 10 Sample Output 99 Here is my code: canva_brush = 40 color_num = int(input()) color_price = color_num * 5 first_total = canva_brush + color_price total = int(first_total + (first_total *(10/100))) print(round(total)) There are two test cases that make my code fail and I don't know why??

22nd Dec 2024, 7:04 AM
Yasmin Sherif
5 Réponses
22nd Dec 2024, 7:13 AM
Bob_Li
Bob_Li - avatar
+ 4
you're converting to int before applying math.ceil. Don't do that. It truncates the float, so you're giving math.ceil the wrong value. round, math.ceil and math.floor needs float, not int. Sorry, I shoul have noticed that error in your code earlier. https://codedamn.com/news/JUMP_LINK__&&__python__&&__JUMP_LINK/converting-float-to-int-in-python total = first_total + (first_total*(10/100)) print(math.ceil(total)) --edit-- Thanks for the clarification by Jan, this is actually a wrong solution. Use round, not math.ceil. Just don't convert to int before rounding. But I still believe that the code coach was misleading in it's use of the term "rounded up".
22nd Dec 2024, 2:15 PM
Bob_Li
Bob_Li - avatar
+ 1
Unfortunately also math.ceil gave me an error😭
22nd Dec 2024, 1:57 PM
Yasmin Sherif
+ 1
It's 5 * input + 40, and then you divide it with 10 and use the normal round and not math.ceil or math.floor.
22nd Dec 2024, 9:26 PM
Jan
Jan - avatar
+ 1
Jan you're right. my mistake...😅 It turns out it is wrong to use math.ceil. The wording threw me off. "rounded up" It should have been "rounded to" Now I feel that the wording of the code coach problem is misleading.... but yes, the int conversion was the source of yasminsherif's error, not the use of round. why "rounded up" is a bad choice of words in the Code Coach: import math print(f'{math.ceil(1.5) = }', 'rounded up') print(f'{round(1.5) = }', 'rounded up') print(f'{math.floor(1.5) = }', 'rounded down') print() print(f'{math.ceil(2.5) = }', 'rounded up') print(f'{round(2.5) = }', 'rounded down <--!!') print(f'{math.floor(2.5) = }', 'rounded down')
23rd Dec 2024, 2:52 AM
Bob_Li
Bob_Li - avatar