+ 1
Python
What is wrong with this code?? import math data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here people = 0 for x in data: for i in x: people += i if color == 'brown': print(math.floor(((data[0][0]+data[1][0]) / people) * 100)) elif color == 'blue': print(math.floor(((data[0][1]+data[1][1]) / people * 100)) elif color == 'green': print(math.floor(((data[0][2]+data[1][2]) / people * 100)) elif color == 'black': print(math.floor(((data[0][3]+data[1][3]) / people * 100))
8 Answers
+ 3
Missing closing brackets in all elif conditions .
+ 1
Yousif yes
+ 1
Yousif what do you mean ? It works fine for me . I don't know about test cases or the question itself , so can't help with that unless you let us know about it!!
+ 1
Yousif Here's how you can shorten your code:
data = ... # the data
people = sum(y for x in data for y in x)
k = ("brown", "blue", "green", "black").index(input())
print((100 * (data[0][k] + data[1][k]) / people) // 1)
# Assuming that I haven't made any error in my answer, I hope that this helps you with your query. Happy coding!
0
Abhay you mean parenthesis?
0
Abhay it did not work :(
0
Abhay You are right, it is not about cases though, it gives me an error message it tells me that my code has syntax error
0
data = [
[23, 11, 5, 14],
[8, 32, 20, 5]
]
color = input()
#your code goes here
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]
sum = brown + blue + green + black
if color == 'brown' :
x = brown / sum
elif color == 'blue':
x = blue / sum
elif color == 'green':
x = green / sum
else:
x = black / sum
x *= 100
print (int (x))
Try this one it really works