Why is this code resulting in error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is this code resulting in error?

I am trying to complete halloween candy code but whenever I run this only the visible test get the expected output other hidden three test dont. And because the tests are hidden i dont know the problem. Please help me. https://code.sololearn.com/c4uNDHFAlXTN/?ref=app

29th Jan 2022, 7:59 AM
D-Minun
D-Minun - avatar
31 Answers
+ 1
Here is a solution without math.ceil(). But there is a condition I am not sure why I need to include. It passes the test, though The rounding problem doesn't even come up. https://code.sololearn.com/cf5dUKP3sfy6/?ref=app why do I need the check for 0.2? This one I don't understand. I just added it to satisfy case 3.
31st Jan 2022, 3:42 AM
Bob_Li
Bob_Li - avatar
+ 2
coding Syntax are write but input format is wrong.
30th Jan 2022, 5:45 PM
RUDRA PRATAP CHAUHAN
RUDRA PRATAP CHAUHAN - avatar
+ 1
It doesn't result in an error, the math is probably wrong though. Include the project description so others can see what the issue is
29th Jan 2022, 8:10 AM
Slick
Slick - avatar
+ 1
You go trick or treating with a friend and all but three of the houses that you visit are giving out candy. One house that you visit is giving out toothbrushes and two houses are giving out dollar bills. Task Given the input of the total number of houses that you visited, what is the percentage chance that one random item pulled from your bag is a dollar bill? Input Format An integer (>=3) representing the total number of houses that you visited. Output Format A percentage value rounded up to the nearest whole number.
29th Jan 2022, 8:16 AM
D-Minun
D-Minun - avatar
+ 1
it says "rounded up to the nearest whole value". Look up something you can use to round up, regardless of the number and use that.
29th Jan 2022, 8:18 AM
Slick
Slick - avatar
+ 1
Yeah, thats the wrong function. You gotta look for one that just rounds up. The ceiling is up and the floor is down.
29th Jan 2022, 10:31 AM
Slick
Slick - avatar
+ 1
I'm not sure why it doesn't go with the round function but i tried the ceil function and it worked: import math houses = int(input()) tiklu=2/houses*100 print(math.ceil(tiklu))
29th Jan 2022, 8:21 PM
Yulia
Yulia - avatar
+ 1
rounding is not a simple process. the method you use have to be what the problem requires. Imagine if you're dealing with money and you lose $0.001 everytime you round. 😂
30th Jan 2022, 2:56 AM
Bob_Li
Bob_Li - avatar
+ 1
The round method rounds down as well as up... you need a different method one that always rounds up.
30th Jan 2022, 10:42 AM
Gareth Vaughan
Gareth Vaughan - avatar
+ 1
I'm new to python too and had troubles with this task as well. Actually I was looking for rounding methods and found this function in wikipedia😅
30th Jan 2022, 1:49 PM
Yulia
Yulia - avatar
+ 1
Bob_Li ohh now i understand it. Problem might be that when digit after decimal is 5 expected outcome is a proper estimate but in my code it will always add one even if digit on left side of is odd.
30th Jan 2022, 2:37 PM
D-Minun
D-Minun - avatar
+ 1
import math houses = int(input()) def dollarpercent(h): if h>=3: result = int(math.ceil((2/h) * 100)) print(result) dollarpercent(houses) That is why I used math.ceil. Because you always have to round up. But as I said, what method of rounding you use depends on what it is used for.
30th Jan 2022, 2:40 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li I am trying to avoid using ceil function because i have not reached that lesson and i dont know how it works.
30th Jan 2022, 3:04 PM
D-Minun
D-Minun - avatar
+ 1
Bob_Li There is an example of real life application of floor function. Answer to What are the use and difference ceil() and floor() in C and C++? by Ashutosh Singh https://www.quora.com/What-are-the-use-and-difference-ceil-and-floor-in-C-and-C/answer/Ashutosh-Singh-1712?ch=15&oid=101826662&share=b27db422&srid=hhY0Lh&target_type=answer As you said the use depends on situation)
30th Jan 2022, 3:06 PM
Yulia
Yulia - avatar
+ 1
ceil() just deletes the decimal and adds 1 to the integer.. floor(), deletes the decimal and leaves the integer unchanged. This is like simply converting the number to int using int(). The test would not know what you used as long as you get the correct answer. You already learned it here. I would like to see your solution without ceil. Maybe you could add 1 to your result. That might work. Learning is always a good thing.
30th Jan 2022, 3:07 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li are you able to see hidden test cases as you have pro? If yes can you check those and tell me whats the problem?
31st Jan 2022, 1:25 AM
D-Minun
D-Minun - avatar
+ 1
Hidden tests are hidden for me, too. You just have to consider what kind of input might be passed to your code, and modify your code to be able to give the correct output everytime.
31st Jan 2022, 2:44 AM
Bob_Li
Bob_Li - avatar
+ 1
D-Minun int rounds off the value, like 5.5 to , 0.3 to 0. Like I've already mentioned above to use float (number) for working with decimal numbers
31st Jan 2022, 2:47 AM
Saad Khan
Saad Khan - avatar
0
Is this what you want?
29th Jan 2022, 8:16 AM
D-Minun
D-Minun - avatar
0
I tried using round function but it still gives the same result
29th Jan 2022, 8:24 AM
D-Minun
D-Minun - avatar