+ 3

# Python Challenge Code Question(Probability)

Please help me solve this code. There are input() number of houses. But of all the houses, 3 of them give different stuff instead of candies. 1 of the 3 houses gives toothbrushes and 2 of the 3 houses gives dollar bill. What is the probability of picking a dollar bill? My code: houses=input() prob=((2/houses)*100) print(prob) The issue here is, it only worked for just two tests, but the other 3 test failed. How do I get all to pass?

16 Answers

+ 2

Ohene Agyena Karikari Frimpong Just an important hint about this:
"The int(input()) was already add. I ommitted it im this post"
Instead of copying and pasting the code, save it in Code Playground and add a link to it in the question description (use "+" button).
This way, the code is always updated, there's no risk of copy errors, and anyone can easily run and debug it.

+ 6

houses = int( input()) #you must convert input to integer type.

+ 3

Read the task description carefully, the output should be:
"A percentage value rounded up to the nearest whole number."

+ 2

The int(input()) was already add. I ommitted it im this post. It still doesnโt solve the other 3 tests that failed. Jayakrishna ๐ฎ๐ณ

+ 2

I highlighted the relevant word:
"A percentage value rounded **up** to the nearest whole number."

+ 2

You have lost me Jayakrishna ๐ฎ๐ณ I donโt know how to use ceil(). Could you please show me how?

+ 1

This is the actual code I used.
houses = int(input())
#your code goes here
prob=round((2/houses)*100)
print(prob)
The Code Challenge is called Halloween Candy.
Two tests passed with this code and 3 failed. Lisa

+ 1

Wow๐. It worked. Jayakrishna ๐ฎ๐ณ Thank you so much.

+ 1

I think this is Intermidiate level stuff. Iโm currently at Data Structures. I am truly grateful.

+ 1

Input(1/3)

0

Please write me the code you would use to solve this question.Lisa

0

Instead of round, apply ceil().

0

First import math module like :
import math
then
prob = math.ceil( (2/houses) *100 )
print( prob)

0

from math import ceil
houses=int( input() )
prob=ceil((2/houses)*100)
print(prob)
#you can do this way also. You're welcome.

0

Use
houses = int(input())
prob = ((2/(houses-3))*100)
print(prob)
In this version of the code, we subtract 3 from the total number of houses to exclude the 3 houses that give different stuff from the calculation. We then calculate the probability of picking a dollar bill from the remaining houses that give dollar bills, which is 2 out of the total number of houses minus 3. Finally, we multiply the probability by 100 to express it as a percentage.

0

You need to convert the variable 'prob' to an integer,
And make your input an integer,
as int auto returns as a string