+ 17
Halloween Candy
Could someone help me with this problem? houses = int(input()) #your code goes here dollar = 2/houses*100 print(round(dollar)) It works only for the first two cases. The other ones are locked for me, so I don't know what's wrong.
18 Réponses
+ 28
Output Format
A percentage value rounded up to the nearest whole number.
round up is done with. math.ceil() not round()
+ 10
houses = int(input())
dollar = 2/houses*100
from math import ceil
print (ceil(dollar))
+ 9
I fought with this one for almost a week. Totally grasped that the number had to be rounded, but I didn't pay attention that it had to be rounded up. Must pay attention to those details. #rookiemistake
+ 8
This was though. I am a beginner and I dont know the ceil method, but I found another way
houses = int(input())
#your code goes
a = 2/houses*100
if a %1 > 0 and a %1 < 0.5:
b = 1+a
print (round(b))
else:
print (round(a))
+ 5
Your problem is caused because of way the round function works...
It will round for the nearst integer, but in the middle (.5) the function returns the nearest even number
E.g.:
>>round(1.5)
2
Using math.ceil will work in this challenge, but you will be rounding all the numbers to up
+ 4
Jerome Grobbelaar Nice workaround! If you want to use ceil() you just need to import the math pack. The codes above show how it's done (it's not very complicated). Stay safe and happy coding!
+ 2
Thank you! I always thought round() would round up by default. Had the same test result and it drove me nuts. ^^
+ 2
Here another solution without usinga math module:
houses = int(input())
result= (2/houses)*100
if result % 1 !=0:
print(int(result)+1)
else:
print(int(result))
+ 1
I was stuck on this for days too. I thought of using round() but acctually it should be math.ceil()
+ 1
Thanks so much everybody, I was stuck too.
+ 1
Wow thanks a lot
+ 1
Same problem, same mistake, same solution ! Thanks
+ 1
You have to import math first to work...or else it returns an error of variable not found.
Sorry if it it's too basic but for me it was new!! 😑 (1week In the world of programming)
import math
houses = int(input())
perc_bills=(2*100)/houses
print (math.ceil(perc_bills))
0
Can you show us your attempt?
0
public class Main {
public static void main(String[] args) {
System.out.println("Enter no of houses");
Scanner sc = new Scanner(System.in);
int houses = sc.nextInt();
float dollor = 2;
float pod;
pod=(dollor*100/houses);
System.out.println(Math.round(pod));
}
}
0
THARUN REDDY Wrong language (Python tag in the question) ;)
Happy coding!
0
Actually the correct problem shouldn't have round in it ok
Plus the 2*100 comes first before the houses
This is how it works
import math
houses= int(input())
print (math.ceil(2*100/houses))
I used only 3 lines and all cases ticked
0
Using only what was taught in the intro to python course
houses = int(input())
perc = round((2/houses)*100,2)
if perc - int(perc)==0:
print(int(perc))
else:
print(int(perc+1))