+ 29

Code coach python Halloween candy

I am quite a beginner, and I am doing the Halloween candy challenge. And I have a problem, the goal is to calculate the chance that you take a dollar bill out of your pocket. For the whole thing search it up for yourself.) The code that I've written is: houses = int(input()) x = 2 / houses * 100 print(round(x)) and when I run the code it says 2 out of five right. But those two are the only ones I can see. So I don't know what to improve. Please help me by explaining it.

23rd Dec 2019, 10:22 PM
Mister Moldy
19 Respostas
+ 25
In the output format of the challenge, it mentions "rounded up to the nearest whole number". If you use the round function, it might round down as well in some cases. Use ceil function to round up instead like Alexandr showed you.
23rd Dec 2019, 10:49 PM
r1c5
r1c5 - avatar
+ 15
But only two out of five test cases are correct. The first and the second
3rd Apr 2020, 10:51 PM
Zahra P
Zahra P - avatar
+ 14
houses = int(input()) import math if houses >= 3: p = 200 / houses print (int(round(p,0)))
3rd Apr 2020, 10:49 PM
Zahra P
Zahra P - avatar
+ 13
Ok, thank you MaxTheMachine🌸
3rd Apr 2020, 11:20 PM
Zahra P
Zahra P - avatar
+ 8
Try this houses = int(input()) a = 200.0/houses q=int(a) p=a-q if p>0.0 : print(q+1) else : print(q)
29th Apr 2020, 9:28 AM
xkcd
xkcd - avatar
+ 7
THX for the question and the answer, Alexandr. Had the same approach using "round"
26th Dec 2019, 2:33 PM
MaxTheMachine [PRO]
MaxTheMachine [PRO] - avatar
+ 7
Mister moldy Did you solve this question? I have exactly the same problem as you. Help me please.
3rd Apr 2020, 10:34 PM
Zahra P
Zahra P - avatar
+ 7
CHECK THIS OUT:-) houses = int(input()) import math if houses>=3: dollars = 200/houses answer = math.ceil(dollars) print(answer)
25th May 2020, 8:31 AM
Sri Siva Murugan V
Sri Siva Murugan V - avatar
+ 6
I missed the word “up” in the instructions and have been so confused until now🤦‍♂️ Thank you!!
26th Dec 2019, 9:47 AM
InfiniteNoob365
+ 6
Zahra P , use Alexandr's solution (first answer): import math math.ceil(x)
3rd Apr 2020, 10:47 PM
MaxTheMachine [PRO]
MaxTheMachine [PRO] - avatar
+ 5
Thank you for the question and answer! Had the same issue
30th Dec 2019, 7:51 AM
Danyal Ahmed
Danyal Ahmed - avatar
+ 5
Task is to "round up". It's not possible with "round". Therefore you need to "math.ceil" instead of round after you imported math. math.ceil(p)
3rd Apr 2020, 11:10 PM
MaxTheMachine [PRO]
MaxTheMachine [PRO] - avatar
+ 4
Yeah, I got it now. Thanks to you both.
23rd Dec 2019, 10:51 PM
Mister Moldy
+ 3
here’s mine: import math if houses > 2: x= math.ceil((2/houses)*100) x= (int(x))
22nd Apr 2020, 2:30 AM
YUNG MOON
YUNG MOON - avatar
+ 3
The only way I found : houses = int(input()) p = 200/houses import math d = math.ceil(int(p) + 0.5) if p == 20: print("20") else: print(d) Yeah I know, that's not how it has to be but btw it works.
28th Sep 2020, 2:09 PM
BayaZ
BayaZ - avatar
+ 2
Hello, by the time I solved this I didn't know nothing about "import math". I still don't know, I solved it like 3 minutes ago. I used this approach. Basically I used list function to grab the last number of float and if it was bigger than 0 I added 1 to the chance, then used int to reduce it to int. I hope I explained it well. houses = int(input()) one_percent = houses / 100 chance = 2 / one_percent chance = str(chance) round_extract = int(chance[-1]) chance = float(chance) if round_extract > 0: chance = chance + 1 chance = int(chance) else: chance = int(chance) print(chance)
21st May 2023, 10:53 AM
Jan Všetíček
0
also if you want to use round number I recommend adding + 0.4 to the final float before printing
21st May 2023, 11:09 AM
Jan Všetíček
0
import math houses = int(input()) #a= no.of houses giving dollors a=2 #c=percentage chance of getting dollors c=(a/houses)*100 if c is int: print(c) else: print(math.ceil(c)) When you round off the answer it might round off to a lesser number. (50.1% is rounded off to 50%) but according to the problem it needs to be rounded off as 51%. So, that's where it went wrong. Try importing math and use the ceil function (greatest integer function )
15th Jul 2023, 10:25 AM
Shiny Diavajna Ponnamalla
Shiny Diavajna Ponnamalla - avatar