root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Name''Names')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
for layer in group.children():
score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
formula_score4.append(score4)
for layer in group.children():
score3a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 3, 0.00)*3 OR '
formula_score3a.append(score3a)
for layer in group.children():
score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
formula_score3.append(score3)
for layer in group.children():
score2a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 2, 0.00)*2 OR '
formula_score2a.append(score2a)
for layer in group.children():
score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
formula_score2.append(score2)
for layer in group.children():
score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
formula_score1.append(score1)
formxformula1 = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
new_formformula2 = formxformula1.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
new_form2formula3 = new_formformula2.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
new_form3formula4 = new_form2formula3.replace("OR THEN 2 ", "THEN 2 ")
formulafinal_formula = new_form3formula4.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
formula
del formula_score4[:]
del formula_score3a[:]
del formula_score3[:]
del formula_score2a[:]
del formula_score2[:]
del formula_score1[:]


'CASE WHEN "Layer_1a_Score" = 4 OR
"Layer_1b_Score" = 4 OR
"Layer_1c_Score" = 4 OR
"Layer_1d_Score" = 4 OR
"Layer_1e_Score" = 4 OR
"Layer_1f_Score" = 4 OR
coalesce("Layer_1a_Score"Layer_1a = 3, 0.00)*3 OR
coalesce("Layer_1b_Score"Layer_1b = 3, 0.00)*3 OR
coalesce("Layer_1c_Score"Layer_1c = 3, 0.00)*3 OR
coalesce("Layer_1d_Score"Layer_1d = 3, 0.00)*3 OR
coalesce("Layer_1e_Score"Layer_1e = 3, 0.00)*3 OR
coalesce("Layer_1f_Score"Layer_1f = 3, 0.00)*3 >=9
THEN 4 WHEN
"Layer_1a_Score" = 3 OR
"Layer_1b_Score" = 3 OR
"Layer_1c_Score" = 3 OR
"Layer_1d_Score" = 3 OR
"Layer_1e_Score" = 3 OR
"Layer_1f_Score" = 3 OR
coalesce("Layer_1a_Score"Layer_1a = 2, 0.00)*2 OR
coalesce("Layer_1b_Score"Layer_1b = 2, 0.00)*2 OR
coalesce("Layer_1c_Score"Layer_1c = 2, 0.00)*2 OR
coalesce("Layer_1d_Score"Layer_1d = 2, 0.00)*2 OR
coalesce("Layer_1e_Score"Layer_1e = 2, 0.00)*2 OR
coalesce("Layer_1f_Score"Layer_1f = 2, 0.00)*2 >=6
THEN 3 WHEN
"Layer_1a_Score" = 2 OR
"Layer_1b_Score" = 2 OR
"Layer_1c_Score" = 2 OR
"Layer_1d_Score" = 2 OR
"Layer_1e_Score" = 2 OR
"Layer_1f_Score" = 2
THEN 2 WHEN
"Layer_1a_Score" = 1 OR
"Layer_1b_Score" = 1 OR
"Layer_1c_Score" = 1 OR
"Layer_1d_Score" = 1 OR
"Layer_1e_Score" = 1 OR
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...