Halloween Candy Challenge final 3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Halloween Candy Challenge final 3

I am having issues with the final 3 that it won’t show you the numbers for. My code works perfectly and rounds to the nearest whole number. That’s why I don’t understand why it isn’t working. #input will be an int (>=3) houses = int(input()) #get the float chance to pull a dollar chance = (2 / houses) * 100 decimal = chance #gets only the numbers after the decimal place while decimal >= 1: decimal -=1 #adds one to chance only if 0.5 or greater if decimal >= 0.5: chance += 1 #print the chance(ex. 3.9 will print 3, so will 3.49) print(int(chance)) I’ve seen people using the math import and ceil, but i did the same concept and it didn’t work for the challenge. I’m confused because I tried to make mine as basic as possible. Is there something else the ceil does that I don’t know about?

15th Jan 2020, 4:20 PM
MadiSperry
MadiSperry - avatar
9 Answers
+ 3
I just wrote a little test. Copypaste, run it and see for yourself: The results differ! from math import ceil def candy(n): houses = n chance = (2 / houses) * 100 print('ceil:', ceil(chance)) decimal = chance while decimal >= 1: decimal -=1 if decimal >= 0.5: chance += 1 print('freestyle:', int(chance), '\n') for n in range(1, 101): candy(n)
15th Jan 2020, 4:30 PM
HonFu
HonFu - avatar
+ 1
int basically just cuts the decimals off. int(3.7) -> 3 ceil(3.7) -> 4
15th Jan 2020, 4:22 PM
HonFu
HonFu - avatar
+ 1
thanks HonFu, i actually started to understand it afterward
15th Jan 2020, 4:55 PM
MadiSperry
MadiSperry - avatar
+ 1
somehow i cant solve this challenge, after see your conversation about ceil i think i understand why any of my solution isnt working. it looks like i misunderstood the question and only took "nearest whole number" as the problem i need to solve instead of "round up the number"
15th Jan 2020, 5:09 PM
Taste
Taste - avatar
0
thanks HonFu, but doesnt my while and if loop do the same? for instance when the number 3 is put mine still shows 67%... its only rhe bottom three that dont work
15th Jan 2020, 4:24 PM
MadiSperry
MadiSperry - avatar
0
I just ran it again and made a screenshot. Look closely at the numbers: Some do differ! https://www.sololearn.com/post/220328/?ref=app
15th Jan 2020, 4:48 PM
HonFu
HonFu - avatar
0
yeah, so if its 3.4 you need 3 and if its 3.6 you need 4
15th Jan 2020, 5:11 PM
MadiSperry
MadiSperry - avatar
0
A lot of people missed that, in several tasks.
15th Jan 2020, 6:03 PM
HonFu
HonFu - avatar
0
د
15th Jan 2020, 11:43 PM
Mehdi heydari
Mehdi heydari - avatar