0
Halloween Candy challenge
Is it possible to solve this problem without ceil? Given: Output Format A percentage value rounded up to the nearest whole number. Input type is an integer >= 3, My logic was as follows: houses = int(input()) #your code goes here percent = 200 // houses if houses > 200: print(1) elif houses % 2 == 0: print(percent) else: print(percent +1) It works for all cases but one. What am I missing?
5 Réponses
+ 3
Ярослав Вернигора(Yaroslav Vernigora) That would be normal rounding, what is needed is to round up if there is any fractional part.
Leandro Saltarelli
You could do something like this;
houses = 200 / int(input())
print(int(houses) if houses == int(houses) else int(houses) + 1)
You just need to check if the integer value is equal to the value of the percentage. If not then take the integer value plus 1 to round up. If yes, then output the integer value of the percentage.
+ 1
Yes, but you'll still need to round up. So, you'd need to write some additional code to handle that.
0
Thanks ChaoticDawg. But if I'm using floor division, shouldn't my else statement take care of the rounding up?
0
Hi! I read that you can round it up like this: add 0.5 to the int type
0
I figured it out!
The problem was my elif statement.
200 % n won't always be 0 just because n is even. And there's also 5 and 25 which are odd but shouldn't lead to rounding up if entered as input in this case.
So this is my solution that works using just the content that I've learned so far in the py course up to intermediate :
houses = int(input())
#your code goes here
percent = 200 // houses
if houses > 200:
print(1)
elif 200 % houses == 0:
print(percent)
else:
print(percent +1)