Python – SoloLearn Python Data Structures, Apple Of My Eye

I finished the ‘Python Intermediate’ course and immediately started up ‘Python Data Structures’, got to this fun problem in the ‘List’ section. Data structures are typically my strength., but I may not yet be super efficient. They work though.

Basically we are making a program to accept a user input of one of four eye colors, males and females in this group, then we will output the percentage of that eye color in the group.

This one was a lil tricky, but this below was my working solution. So I created a ‘colors’ list in proper order to get the index needed like if the input was ‘blue’ then that index is ‘1’ for both males and females. Then with color index found now add the sum(data[0][1]) + sum(data[1][1]) to get the male + females total color count for blue, then just do the percentage math.

Updated add a little more help due to some questions, remember the first item in a list starts with 0 then 1. So if I asked you in data[ ] what is the second number in the first list (males) the correct answer would be data[0] [1] = 11. List help.

data = [
  # Count of eye colors for both male then females from a imaginary company.
  #males
  [23, 11, 5, 14],
  #females
  [8, 32, 20, 5]
]
# to enter a color of brown, blue, green, or black
color = input()

#your code goes here

total = sum(data[0]) + sum(data[1])

# the two nested lists data[0] and data[1] use the color order below 0 = brown, 1 = blue etc.
colors = ['brown', 'blue', 'green', 'black']

# the colors.index(color) code below return one of the list index numbers 0,1,2,3
# so if you inputted blue for the males it would look just like data[0][1] 
color_total = data[0][colors.index(color)] + data[1][colors.index(color)]

print(int((color_total / total) * 100))

Leave a Comment