0
standards = ['1', '2', '3']
sections = [['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C']]

classes_maker_counter = 0
for i in standards:
    for j in sections[classes_maker_counter]:
        for k in j:
            classes = f"alloted_{i}_{k}= {}"
            todisplay = f"to_display_{i}_{k} = {}"
            exec(classes)
            exec(todisplay)
    classes_maker_counter += 1

when i run this i get this error

    f-string: empty expression not allowed

is there a way to do this...

Jayesh B
  • 3
  • 5
  • 2
    Could you be more specific about the desired output? – Snedecor Oct 20 '19 at 12:03
  • Yes, there is - although I question your reasons for wanting to do this! – Nick Martin Oct 20 '19 at 12:07
  • 1
    This is a bad idea. Use a dict instead, see the duplicate for more details and solutions. – Thierry Lathuille Oct 20 '19 at 12:10
  • I voted for the dupe (`...variable number of variables`) - that's the way you should do it - constructed strings as keys in a dictionary but... Related: [How can I print literal curly-brace characters in python string and also use .format on it?](https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-python-string-and-also-use-fo) – wwii Oct 20 '19 at 13:11

1 Answers1

1

I should say that this seems like a terrible idea, but to make your code work you need to create a separate string for your "empty dictionary" that isn't an f-string. Like this:

classes = f"alloted_{i}_{k} = " + "{}"
todisplay = f"to_display_{i}_{k} = " + "{}"

Quite how you plan on using/accessing these variables later is another question entirely... but this is what you asked for!

Nick Martin
  • 731
  • 3
  • 17