+ 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
31 Réponses
+ 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.
+ 2
coding Syntax are write but input format is wrong.
+ 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
+ 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.
+ 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.
+ 1
Yeah, thats the wrong function. You gotta look for one that just rounds up. The ceiling is up and the floor is down.
+ 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))
+ 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. 😂
+ 1
The round method rounds down as well as up... you need a different method one that always rounds up.
+ 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😅
+ 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.
+ 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.
+ 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.
+ 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)
+ 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.
+ 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?
+ 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.
+ 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
0
Is this what you want?
0
I tried using round function but it still gives the same result