+ 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.
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.
+ 15
But only two out of five test cases are correct. The first and the second
+ 14
houses = int(input())
import math
if houses >= 3:
p = 200 / houses
print (int(round(p,0)))
+ 13
Ok, thank you MaxTheMachine🌸
+ 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)
+ 7
THX for the question and the answer, Alexandr. Had the same approach using "round"
+ 7
Mister moldy
Did you solve this question? I have exactly the same problem as you. Help me please.
+ 7
CHECK THIS OUT:-)
houses = int(input())
import math
if houses>=3:
dollars = 200/houses
answer = math.ceil(dollars)
print(answer)
+ 6
I missed the word “up” in the instructions and have been so confused until now🤦♂️ Thank you!!
+ 5
Thank you for the question and answer!
Had the same issue
+ 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)
+ 4
Yeah, I got it now. Thanks to you both.
+ 3
here’s mine:
import math
if houses > 2:
x= math.ceil((2/houses)*100)
x= (int(x))
+ 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.
+ 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)
0
also if you want to use round number I recommend adding + 0.4 to the final float before printing
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 )