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

Help (Halloween Candy)

houses = int(input()) #your code goes here porcentaje = round ((2 / houses)*100) print (porcentaje) This is my code in Python, it is accepted by the first two test cases, but the hidden cases give errors. Please help

4th Feb 2024, 4:11 AM
Dayana Alvarez
Dayana Alvarez - avatar
5 Answers
+ 4
Hi Dayana Alvarez Instead of round() u can import math module and use math.ceil() and check if it works
4th Feb 2024, 5:26 AM
𝘕𝘉
𝘕𝘉 - avatar
+ 3
🌀NB🌀 Rain thanks, I think I understood, round() to round to the nearest integer and math.ceil() to round up
5th Feb 2024, 5:24 AM
Dayana Alvarez
Dayana Alvarez - avatar
+ 1
Dayana Alvarez , Yeah, you have to round up, not round to the nearest.
5th Feb 2024, 1:28 AM
Rain
Rain - avatar
+ 1
Here's my code, not need to import modules. houses = int(input()) dollar_p = (2 / houses) * 100 rounded = dollar_p if dollar_p == int(dollar_p) else dollar_p + 1 if houses >= 3: print(int(rounded)) The rounded part check if dollar_p is an interger, if it's not it add +1 to dollar_p... In the print function, I added "int" to make the float and integer and essentially rounding up the result. It could be improved by making sure rounded is always an integer in the assignment of "rounded" but this works perfectly.
20th Mar 2024, 4:17 AM
Karl Breault
Karl Breault - avatar
0
It worked adding 0.5, although I didn't understand much. For example, if there are 15 houses, the result is 14, but before rounding it is 13.333333 which is closer to 13. I thought the round() function was responsible for rounding to the nearest integer
4th Feb 2024, 4:58 AM
Dayana Alvarez
Dayana Alvarez - avatar