 How can I shorten my endless if-statements in this question? | Sololearn: Learn to code for FREE!

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))

+1

colors = "brown blue green black".split() colors = { "male": { colors[i]: data[i] for i in range(4) }, "female": { colors[i]: data[i] for i in range(4) }, } all_people = colors["male"][color] + colors["female"][color] print(int(100*all_people/118))

+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[i] for i in range(4)}, "female":{colorz[i]:data[i]for i in range(4)} } eye_color_people=colors["male"][color]+colors["female"][color] print(int(100*eye_color_people/118))

+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.

+1

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[colorList.index(color)] + data [colorList.index(color)]) /118*100 ) print(percentage) else: print("Sorry Not Found")

+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+data sum_br = (br/gr)*100 bl = data+data sum_bl = (bl/gr)*100 gre = data+data sum_gre = (gre/gr)*100 blac = data+data 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))

+1

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() if color == "brown": color = (data + data)/118*100 print(round(color)) elif color == "blue": color = (data + data)/118*100 print(round(color)) elif color == "green": color = (data + data)/118*100 print(round(color)) elif color == "black": color = (data + data)/118*100 print(round(color))

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+ data)) elif color == "blue": return ((11 + 32) / sum(data + data)) elif color == "green": return ((5 + 20) / sum(data + data)) else : return ((19) / sum(data + data)) per = percentage(color) print(int(per * 100))

-1

Ok, first you need to generalize your code by substituting each number with the corresponding entry in data (eg 23->data) 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

-1

That sounds like an idea. I'll try it. Thanks

-1

color = input("Say Eye color of the list: brown, blue, green, black") if color == "brown": colorEyed = int(((data + data)/118)*100) elif color == "blue": colorEyed = int(((data + data)/118)*100) elif color == "green": colorEyed = int(((data + data)/118)*100) elif color == "black": colorEyed = int(((data + data)/118)*100) else: print("Not an indexet color") breakpoint() print(("The percentage of " + "eyes color " + color + " is "+ str(colorEyed)))

-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.

-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[pos] + data[pos]) / (sum(data) + sum(data))) print(per)

-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)+sum(data) percetn = dic[color]/total*100 print (int(percetn))

-1

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] total_people = sum(data)+sum(data) 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[n] + data[n] percentage = (total_color_eyes/total_people)*100 print(int(percentage))

-1

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() brown = data + data blue = data + data green = data + data black = data + data 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))

-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+data) if color == "blue": tcolor = (data+data) if color == "green": tcolor = (data+data) if color == "black": tcolor = (data+data) print ( int((tcolor/SumP)*100))

-2

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] total_sum_of_list = (sum(data+data)) color = input() colour = "brown blue green black".split() male = {colour[i]:data[i] for i in range(4)} female = {colour[i]:data[i] for i in range(4)} total_sum_of_colorx = (male[color] + female[color]) print(int((total_sum_of_colorx/total_sum_of_list)*100))

-2

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() total= sum(data+data) if color == "brown": num = int((data+data)*100/total) elif color == "blue": num = int((data+data)*100/total) elif color == "green": num = int((data+data)*100/total) else: color == "black" num = int((data+data)*100/total) print(num) Not quite the best solution here but it works.

-2

Here is my code,(it's long than supposed to be but it works perfectly) data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here total=0 for i in data: for j in i: total+=j sum=0 percentage=0 if color =="brown": sum=data+data percentage=int((sum*100)/total) print(percentage) elif color =="blue": sum=data+data percentage=int((sum*100)/total) print(percentage) elif color =="green": sum=data+data percentage=int((sum*100)/total) print(percentage) else: sum=data+data percentage=int((sum*100)/total) print(percentage)