+ 2
Halloween Candy Challenge
Hi, I'm new to python and I wrote this before knowing about the function math.ceil(), but I keep getting 2/5 right. I can't tell why. if(200%houses)>houses/2: print(int(200/houses+1)) else: print(int(200/houses))
6 Answers
+ 3
you are using 200, and I donât quite know why.
houses = int(input())
dollars = 2
import math
#your code goes here
result = 100/(houses / dollars)
print(math.ceil(result))
this was mine
+ 3
oh but to answer your question, no I donât know :) I like to think of those hidden tests I fail as a way of telling me: âhey, go look at your code again, and expecially look at the case/instructions againâ
that is where I mess up usually, not reading the instructions carefully enough đ
just a friendly tip as well: in general, when you notice yourself writing things such as 200/houses several times, it is best to put them in a variable, or at least try and look to see if you can use a different way of writing it. same goes for the +1
and you can try and look at the simplest/smallest way of looking at stuff. (something I am learning at the moment) dividing 200/2 is the same as dividing 100/1 :)
+ 1
well yeah. you need to use ceil because the challenge states that you need to round up to the nearest integer
0
Hi @Brave Tea,
I know I could have used the math.ceil() function, but by is my code wrong. I used 200 because 2 (dollars)* 100=200. It is exactly what you have too. Does anyone know what the 3-5 tests are?
0
Hi Brave Tea and Sunny Zhou . I think Sunny Zhouâs attempt was to generate the ceil function itself, which means she was trying, using only ints and basic operations, to get to the solution. I am trying the same challenge in Java and have tried the same thing, and got the same tests wrong.
Even so, there is so ething you arent considering in your code, Sunny, its that depending on the parity of houses, houses / 2 can either match the intended value (if houses is even ) or the integer part of the value (if houses is odd). Nevertheless, even applying that, I got the same tests wrong, so I really dont have much of a clue. this post was from 4 months ago. Have tou solved it in the meanwhile?
0
here is my code that works without import any module:
houses = int(input())
result= (2/houses)*100
if result % 1 !=0:
print(int(result)+1)
else:
print(int(result))