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, I have not looked yet but I believe you can do this better with a for loop. 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’. Then with color index found now add the sum(data) + sum(data) to get the male + females total color count for blue, then just do the percentage math.
data = [ #males [23, 11, 5, 14], #females [8, 32, 20, 5] ] color = input() #your code goes here total = sum(data) + sum(data) colors = ['brown', 'blue', 'green', 'black'] color_total = data[colors.index(color)] + data[colors.index(color)] print(int((color_total / total) * 100))