0

# Please correct my code on python (paint costs challenge.)

Out of 5 my code passed 3 I don't found why it stuck on 2 questions please help me in correcting this https://code.sololearn.com/cjtHa8MQLOw3/?ref=app

22 Antworten

+ 1

Import math
colors = int(input())
cost = ((colors * 5.00) + 40.00)
total = ((cost * .10) + cost)
print(math.ceil(total))

+ 8

round() worked for me
https://code.sololearn.com/c5y8n9hR4476

+ 7

You can use math.ceil() to round up to the next integer

+ 6

Output Format
A number that represents the cost of your purchase rounded up to the nearest whole number.
This is where your code is incorrect - it says to round up to the nearest whole number. Yours uses 'int' which simply removes any decimal places (and thus rounds down).

+ 2

Say you need 3 colours, that would be a cost of 3 x 5.00, plus 40.00, which makes 55.00, and then with tax added, gives a total cost of 60.50. This would mean that you would need 61 in order to pay for everything (since the output is a whole number).
Your code outputs int(cost) which means that it is taking your total of 60.50, and taking off the decimal place, so int(60.50) will return 60. That wouldn't be enough to buy all the paints you need.

+ 2

round will round to the nearest whole number. If your total cost worked out at 30.75 for example, round will take it to 31 - great!
But if the cost is 30.25, round will take it down to 30, which obviously isn't enough.

+ 2

For some reason, when having a decimal value of x.5, round will round up when the last digit of x is >= 5, and down otherwise, so round(3.5) -> 3 and round(17.5) -> 18. Can't really explain why as it doesn't make sense to me, sorry.
Either way, I'm trying to say that round is not a suitable method to use for this task. See if you can think of a workaround.

+ 2

If x > x.0
Return(x + 1)
It can be done like this?

+ 2

Not in that syntax, but you've certainly got the right idea. Think about using int() again and how that behaves.
A hint if you'd like; if x has a value of 20.00, then x == int(x) returns True, but if x's value is 20.01, x == int(x) returns False. Your idea of x + 1 is absolutely on the right track.

+ 2

David Ashton Interesting... Mine didn't (fails Case 3). I noticed that you multiply by 1.1 but I multiply by 11/10.
After doing some digging, there are some numbers (ending in 5) where multiplying by 1.1 gives a number ending in .5, and others give an answer ending in .00000000001 (or similar). Also, rounding a number ending in .5 seems to round to the nearest even number. I guess it was quite lucky that round() worked for all cases!

+ 2

Yes, 0.1 is 10% which you can add on to your orignal value. This is the same as multiplying by 1.1.
Example: to add 10% on to 60;
60 * 0.1 = 6, then 60 + 6 = 66
or;
60 * 1.1 = 66
Same answer.

+ 2

You can use ceil function from math module....
#------------------------------------
import math
n=int(input(""))
x=n*5+40
y=0.1*x
t=x+y
print(math.ceil(t))

+ 1

I didn't understand please explain more deeply
instead of int i used round it worked but it failed in one teat i.e. test3

+ 1

Means if 30.25 is the cost so according to question it must 31 m i right?

+ 1

i used round and used the input as 3 then also my output is 60 why not 61?
https://code.sololearn.com/cjtHa8MQLOw3/?ref=app

+ 1

Yep, it the cost is 30.25, you would need 31 to pay for it all.

+ 1

So please sir explain me the right way to do this challenge

+ 1

First I will try and get you to think of a way around this, then I will show you the quick way for it.
Think that if the cost is say 20.00, you want to return 20, but if it is 20.01, you need to return 21. In Python 'if 20.00 == 20:' will return True and 'if 20.01 == 20:' returns False. Can you see any way of using that to help you?

+ 1

Yes yes i understood

+ 1

My trick to round up instead of down without a library is abs(int(x * -1))