Python Challenge Code Question(Probability) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

1st Apr 2023, 9:06 AM
Ohene Agyena Karikari Frimpong
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.
3rd Apr 2023, 1:20 AM
Emerson Prado
Emerson Prado - avatar
+ 6
houses = int( input()) #you must convert input to integer type.
1st Apr 2023, 9:29 AM
Jayakrishna 🇮🇳
+ 3
Read the task description carefully, the output should be: "A percentage value rounded up to the nearest whole number."
1st Apr 2023, 9:52 AM
Lisa
Lisa - avatar
+ 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 🇮🇳
1st Apr 2023, 9:32 AM
Ohene Agyena Karikari Frimpong
+ 2
I highlighted the relevant word: "A percentage value rounded **up** to the nearest whole number."
1st Apr 2023, 10:11 AM
Lisa
Lisa - avatar
+ 2
You have lost me Jayakrishna 🇮🇳 I don’t know how to use ceil(). Could you please show me how?
1st Apr 2023, 10:39 AM
Ohene Agyena Karikari Frimpong
+ 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
1st Apr 2023, 9:58 AM
Ohene Agyena Karikari Frimpong
+ 1
Wow🎉. It worked. Jayakrishna 🇮🇳 Thank you so much.
1st Apr 2023, 10:44 AM
Ohene Agyena Karikari Frimpong
+ 1
I think this is Intermidiate level stuff. I’m currently at Data Structures. I am truly grateful.
1st Apr 2023, 10:46 AM
Ohene Agyena Karikari Frimpong
+ 1
Input(1/3)
2nd Apr 2023, 8:13 PM
Adewale Daniel
Adewale Daniel - avatar
0
Please write me the code you would use to solve this question.Lisa
1st Apr 2023, 10:13 AM
Ohene Agyena Karikari Frimpong
0
Instead of round, apply ceil().
1st Apr 2023, 10:36 AM
Jayakrishna 🇮🇳
0
First import math module like : import math then prob = math.ceil( (2/houses) *100 ) print( prob)
1st Apr 2023, 10:41 AM
Jayakrishna 🇮🇳
0
from math import ceil houses=int( input() ) prob=ceil((2/houses)*100) print(prob) #you can do this way also. You're welcome.
1st Apr 2023, 12:29 PM
Jayakrishna 🇮🇳
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.
2nd Apr 2023, 4:31 PM
Talemwa Ian
Talemwa Ian - avatar
0
You need to convert the variable 'prob' to an integer, And make your input an integer, as int auto returns as a string
3rd Apr 2023, 3:07 AM
K''''
K'''' - avatar