Can someone help me notice what's wrong in my code? Thanks! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me notice what's wrong in my code? Thanks!

Here's the problem : You are given a 2D matrix, which represents the number of people in a room, grouped by their eye color and gender. The first row represents the male gender, while the second row represents females. The columns are the eye colors, in the following order: brown, blue, green, black data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] PY The data shows that, for example, there are 20 green eyed females in the room (2nd row, 3rd column). Our program needs to take eye color as input and output the percentage of people with that eye color in the room. Sample Input: blue Sample Output: 36 Explanation: There are 11+32=43 people with blue eyes, which is 36% of the total. Here's my code : data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here x = data [0] y = data[1] brown = (x[0]+y[0]) blue = (x[1]+y[1]) green = (x[2]+y[2]) black = (x[3]+y[3]) print int((color/(sum x + sum y))

26th Aug 2021, 2:57 PM
Raphaël Leblanc
Raphaël Leblanc - avatar
4 Answers
+ 2
Raphaël Leblanc , i did a try, but i am missing an information. so color "blue" should give a result of 36. this result must be a rounded value, as the real result is a float (36.440677966101696). how should this "rounding" be done: - using int() - using round() - using ceil() - ....
26th Aug 2021, 6:42 PM
Lothar
Lothar - avatar
+ 1
Hi Raphaël! There are many mistakes to mention here. I give you some more details to achieve this practice. You could shorten your code by putting the acceptable inputs(colors) into a list. Then, use a for loop to iterate through the list. When a match is found with the input value, sum the male and female values and print the average. It's better to refer lessons about if-else statements, comparing strings and list functions once.
26th Aug 2021, 3:38 PM
Python Learner
Python Learner - avatar
+ 1
Thanks Python Learner for your time, I will dig in that direction.
26th Aug 2021, 6:54 PM
Raphaël Leblanc
Raphaël Leblanc - avatar
0
Hi Lothat, I think you need to convert the result with int() so it'll be a rounded value
26th Aug 2021, 6:53 PM
Raphaël Leblanc
Raphaël Leblanc - avatar