Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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))

25th Aug 2021, 1:51 AM
Yousif R Wali
Yousif R Wali - avatar
8 Answers
+ 3
Missing closing brackets in all elif conditions .
25th Aug 2021, 2:36 AM
Abhay
Abhay - avatar
+ 1
Yousif yes
25th Aug 2021, 3:33 AM
Abhay
Abhay - avatar
+ 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!!
25th Aug 2021, 3:54 AM
Abhay
Abhay - avatar
+ 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!
25th Aug 2021, 4:35 AM
Calvin Thomas
Calvin Thomas - avatar
0
Abhay you mean parenthesis?
25th Aug 2021, 3:29 AM
Yousif R Wali
Yousif R Wali - avatar
0
Abhay it did not work :(
25th Aug 2021, 3:51 AM
Yousif R Wali
Yousif R Wali - avatar
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
25th Aug 2021, 4:35 AM
Yousif R Wali
Yousif R Wali - avatar
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
26th Aug 2021, 8:59 AM
Ismael Wesa