0

How can I shorten my endless if-statements in this question?

List Operations 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. Our program needs to take eye color as input and output the percentage of people with that eye color in the room. data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] My spaghetti code that works: color = input() if color == "brown": color_male = 23 if color == "blue": color_male = 11 if color == "green": color_male = 5 if color == "black": color_male = 14 if color == "brown": color_female = 8 if color == "blue": color_female = 32 if color == "green": color_female = 20 if color == "black": color_female = 5 all_people = color_male + color_female percentage = (all_people/118)*100 print(int(percentage))

22nd Feb 2021, 1:17 PM
Max_Mnemo
Max_Mnemo - avatar
24 Answers
+ 6
DRY do not repeat yourself data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here colorList = ["brown" ,"blue", "green", "black"] if color in colorList: percentage = round((data[0][colorList.index(color)] + data [1][colorList.index(color)]) /118*100 ) print(percentage) else: print("Sorry Not Found")
24th Sep 2021, 10:51 AM
Shad Abdullah
Shad Abdullah - avatar
+ 1
colors = "brown blue green black".split() colors = { "male": { colors[i]: data[0][i] for i in range(4) }, "female": { colors[i]: data[1][i] for i in range(4) }, } all_people = colors["male"][color] + colors["female"][color] print(int(100*all_people/118))
22nd Feb 2021, 1:43 PM
visph
visph - avatar
+ 1
This one worked for me! data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #Added codes by Emmanuel colorz="brown blue green black".split() colors={ "male":{colorz[i]:data[0][i] for i in range(4)}, "female":{colorz[i]:data[1][i]for i in range(4)} } eye_color_people=colors["male"][color]+colors["female"][color] print(int(100*eye_color_people/118))
24th Apr 2021, 1:57 PM
Emmanuel Massawe
Emmanuel Massawe - avatar
+ 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here key = {"brown": 0, "blue": 1, "green": 2, "black": 3} soma = sum([data[x][key[color]] for x in range(len(data))]) total = 0 for item in data: for value in item: total += value print(int((soma*100)/total)) That was my solution, I hope it helps.
30th Jul 2021, 1:50 AM
Henrique Ribeiro Dos Santos
Henrique Ribeiro Dos Santos - avatar
+ 1
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] ] color = input() #your code goes here gr = 118 br = data[0][0]+data[1][0] sum_br = (br/gr)*100 bl = data[0][1]+data[1][1] sum_bl = (bl/gr)*100 gre = data[0][2]+data[1][2] sum_gre = (gre/gr)*100 blac = data[0][3]+data[1][3] sum_blac = (blac/gr)*100 if color == 'brown': print(int(sum_br)) elif color == 'blue': print(int(sum_bl)) elif color == 'green': print(int(sum_gre)) else: print(int(sum_blac))
6th Dec 2021, 3:40 PM
Maksym
Maksym - avatar
+ 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() if color == "brown": color = (data[0][0] + data[1][0])/118*100 print(round(color)) elif color == "blue": color = (data[0][1] + data[1][1])/118*100 print(round(color)) elif color == "green": color = (data[0][2] + data[1][2])/118*100 print(round(color)) elif color == "black": color = (data[0][3] + data[1][3])/118*100 print(round(color))
31st Mar 2022, 5:28 PM
Aigul Alymova
Aigul Alymova - avatar
+ 1
I made an extra effort at the totalData part just to make the code flexible if the data array value is changed (without built-in sum() method). But you can delete it and just put 118 if you want to. data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] totalData = 0 for i in range(len(data)): for j in range(len(data[0])): totalData += data[i][j] color = input() col = ['brown','blue','green','black'] if color in col: percentage = round((data[0][col.index(color)]+data[1][col.index(color)])/totalData*100) print(percentage)
13th Jul 2022, 11:48 AM
Andrew G
+ 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here # get total people total = 0 for numbers in data: for i in numbers: total += i # get gender in list male = data[0] female = data[1] # get total gender in list def formulation(eyeColor): sum = 0 if eyeColor == 'brown': sum = female[0] + male[0] elif eyeColor == 'blue': sum = male[1] + female[1] elif eyeColor == 'green': sum = male[2] + female[2] elif eyeColor == 'black': sum = male[3] + female[3] print(int(sum * 100 / total)) # formulation # formulation = int(color * 100 / total) formulation(color)
11th Apr 2023, 11:46 PM
rhntp
rhntp - avatar
0
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here def percentage(colour): if color == "brown": return((23+8)/sum(data[0]+ data[1])) elif color == "blue": return ((11 + 32) / sum(data[0] + data[1])) elif color == "green": return ((5 + 20) / sum(data[0] + data[1])) else : return ((19) / sum(data[0] + data[1])) per = percentage(color) print(int(per * 100))
22nd Jun 2022, 7:44 AM
Ashim Ranjit
0
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here if color == "brown": total_color = 31 if color == "blue": total_color = 43 if color == "green": total_color = 25 if color == "black": total_color = 19 #all_people = color_male + color_female perc = (total_color/118)*100 print(int(perc))
2nd Dec 2022, 8:11 AM
Pareshkumar Chaudhari
0
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = str(input()) #your code goes here liczba = int(0) if color == "brown": liczba = data[0][0] + data[1][0] elif color == "blue": liczba = data[0][1] + data[1][1] elif color == "green": liczba = data[0][2] + data[1][2] elif color == "black": liczba = data[0][3] + data[1][3] wszystkie = int(0) for p in data: for i in p: wszystkie += i wynik = int(liczba/wszystkie * 100) print(wynik)
25th Apr 2023, 2:19 PM
Kapitan Dominik Łempicki
Kapitan Dominik Łempicki - avatar
- 1
Ok, first you need to generalize your code by substituting each number with the corresponding entry in data (eg 23->data[0][0]) Than you could build a dictionary with data so you can just do: color_male = d_data["male"][color] color_female =... From there you can just experiment a little to get the best result
22nd Feb 2021, 1:27 PM
Angelo
Angelo - avatar
- 1
That sounds like an idea. I'll try it. Thanks
22nd Feb 2021, 1:28 PM
Max_Mnemo
Max_Mnemo - avatar
- 1
color = input("Say Eye color of the list: brown, blue, green, black") if color == "brown": colorEyed = int(((data[0][0] + data[1][0])/118)*100) elif color == "blue": colorEyed = int(((data[0][1] + data[1][1])/118)*100) elif color == "green": colorEyed = int(((data[0][2] + data[1][2])/118)*100) elif color == "black": colorEyed = int(((data[0][3] + data[1][3])/118)*100) else: print("Not an indexet color") breakpoint() print(("The percentage of " + "eyes color " + color + " is "+ str(colorEyed)))
19th Mar 2021, 7:50 PM
AureliRV
AureliRV - avatar
- 1
I see what visph is trying to do but it doesn't work when I run it and am currently troubleshooting it. AureliRV's code works but still feels spaghettified to me. Congrats to AureliRV, though.
3rd Apr 2021, 7:26 AM
William Anderson
William Anderson - avatar
- 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() colors = ['brown', 'blue', 'green', 'black'] pos = colors.index(color) per = int(100 * (data[0][pos] + data[1][pos]) / (sum(data[0]) + sum(data[1]))) print(per)
11th Sep 2021, 1:37 PM
Challen Wang
Challen Wang - avatar
- 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here dic ={"brown":31,"blue":43,"green":25,"black":19} total =sum(data[0])+sum(data[1]) percetn = dic[color]/total*100 print (int(percetn))
1st Oct 2021, 7:30 AM
jiaqi chen
jiaqi chen - avatar
- 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] total_people = sum(data[0])+sum(data[1]) color = input() if color == 'brown': n = 0 elif color == 'blue': n = 1 elif color == 'green': n=2 elif color == 'black': n=3 total_color_eyes = data[0][n] + data[1][n] percentage = (total_color_eyes/total_people)*100 print(int(percentage))
2nd Jan 2022, 2:18 AM
Marcelo Goes de Freitas
Marcelo Goes de Freitas - avatar
- 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() brown = data[0][0] + data[1][0] blue = data[0][1] + data[1][1] green = data[0][2] + data[1][2] black = data[0][3] + data[1][3] if color == 'brown': print(round((brown/118)*100)) if color == 'blue': print(round((blue/118)*100)) if color == 'green': print(round((green/118)*100)) if color == 'black': print(round((black/118)*100))
28th Jan 2022, 10:16 AM
Rudy Ruiz
- 2
import numpy as np data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] x = np.array( [[23, 11, 5, 14],[8, 32, 20, 5]]) SumP=np.sum(x) for v in data: if color == "brown": tcolor = (data[0][0]+data[1][0]) if color == "blue": tcolor = (data[0][1]+data[1][1]) if color == "green": tcolor = (data[0][2]+data[1][2]) if color == "black": tcolor = (data[0][3]+data[1][3]) print ( int((tcolor/SumP)*100))
25th Apr 2021, 7:37 AM
Mechmech Mahmoud
Mechmech Mahmoud - avatar