How can i make this code better? I want to avoid repetition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can i make this code better? I want to avoid repetition

if trn == 0: if L == "A": A[N] = "X" elif L == "B": B[N] = "X" elif L == "C": C[N] = "X" elif trn == 1: if L == "A": A[N] = "O" elif L == "B": B[N] = "O" elif L == "C": C[N] = "O"

8th Aug 2020, 4:39 PM
Sudhir Krisna
3 Answers
+ 2
Use a dictionary. Or two. However. Keys could be either "0A" ff. within a single dictionary or "A", "B" ff. within two dictionaries. For two it's little more logic to access the right one. Edit: Or two dictionaries within a list... dict["0A"] or dict0["A"] or arr[0]["A"] -> arr = [ {"A" : "test"} ]; print(arr[0]["A"]);
8th Aug 2020, 4:42 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
This is no code-related but a quiz-question or what? From code perspective I don't care how useless the cases are...
8th Aug 2020, 4:49 PM
Sandra Meyer
Sandra Meyer - avatar
0
I have changed the code, are there any more ways to improve this? S = "E" if trn == 0: S = "X" elif trn == 1: S = "O" if L == "A": A[N] = S elif L == "B": B[N] = S elif L == "C": C[N] = S
8th Aug 2020, 4:45 PM
Sudhir Krisna