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

2/22/2021 1:17:14 PM

Max_Mnemo

21 Answers

New Answer

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

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

+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[0][colorList.index(color)] + data [1][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[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))

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

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

-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

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

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

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

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

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

-2

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] total_sum_of_list = (sum(data[0]+data[1])) color = input() colour = "brown blue green black".split() male = {colour[i]:data[0][i] for i in range(4)} female = {colour[i]:data[1][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

-2

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() total= sum(data[0]+data[1]) if color == "brown": num = int((data[0][0]+data[1][0])*100/total) elif color == "blue": num = int((data[0][1]+data[1][1])*100/total) elif color == "green": num = int((data[0][2]+data[1][2])*100/total) else: color == "black" num = int((data[0][3]+data[1][3])*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[0][0]+data[1][0] percentage=int((sum*100)/total) print(percentage) elif color =="blue": sum=data[0][1]+data[1][1] percentage=int((sum*100)/total) print(percentage) elif color =="green": sum=data[0][2]+data[1][2] percentage=int((sum*100)/total) print(percentage) else: sum=data[0][3]+data[1][3] percentage=int((sum*100)/total) print(percentage)