+ 1
Halloween Candy — Challenge — Help
I'm doing Halloween Candy code coach challenge. Once again, I have passed tests 1 and 2. But I don't seem to pass any other tests within that challenge. This is my code: https://code.sololearn.com/cRq7WmfzaAOg/?ref=app Any guidance?
7 Respuestas
+ 6
Lamron ,
the task description says, that the result has to be *rounded up to the nearest whole number*. so we can not use *round()*.
> we should use *ceil(...)* instead. the function is part of the math module, so we need to import it.
+ 6
Lamron ,
short sample which you can run to see the difference of *round()* and *ceil()*
from math import ceil
num = 12.37
print(f'input number: {num}')
print(f'round(...): {round(num)}')
print(f'ceil(...): {ceil(num)}')
+ 4
round ***up***, not round
+ 3
Check on the task description again, it asks us to "rounded ***up*** to the nearest whole number"
+ 3
If you don't want to use the imported module you can also round up this way (that's what I did because I'm a newbie and I've not yet come across any lesson on importing anything):
houses = int(input())
bills = (2 / houses) * 100
dec = bills - int(bills)
if dec > 0:
bills += 1
print(int(bills))
When you subtract " (int)Bills" from "bills" you're left with only the decimals, so now all you have to do is add "1" to the solution, if the decimals are more than zero.
Hope that helps!
+ 2
Interesting. I thought round() will round it to the nearest whole number
+ 1
Anyways, thanks for guidance!