Halloween Candy challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

3rd Mar 2021, 10:39 PM
Leandro
Leandro - avatar
5 Answers
+ 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.
4th Mar 2021, 1:32 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yes, but you'll still need to round up. So, you'd need to write some additional code to handle that.
3rd Mar 2021, 11:23 PM
ChaoticDawg
ChaoticDawg - avatar
0
Thanks ChaoticDawg. But if I'm using floor division, shouldn't my else statement take care of the rounding up?
3rd Mar 2021, 11:41 PM
Leandro
Leandro - avatar
0
Hi! I read that you can round it up like this: add 0.5 to the int type
4th Mar 2021, 12:03 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
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)
4th Mar 2021, 12:53 AM
Leandro
Leandro - avatar