1

I used the exec() function and the for loop to create various class objects and store them in variables.

I think the variables were created successfully as shown in the vs code log, but when I try to access it to change the value or print it, I get this error (name 'membro_1' is not defined) as if it's not defined

What did i do wrong here? how can i access those variables? Is there another better way to create all these objects?

see the variables defined at left

see the variables defined at left

the code from image

class Membros:
    def __init__(self, tag, pontos=0) -> None:
        self.tag = tag
        self.pontos = pontos

# war_cla_member have a len() of 47
war_cla_members = ['#VYQPR', '#82PP2LL20', '#LP0RVCPLV', '#LUVQQ2G2', '#PRP20LUL', '#8GUY0V92R',
                       '#Y02UVP0UV', '#9U0J8GVJL', '#9P2GGVR9Y', '#20QRJLVU8', '#QRYPRQGP9', '#8GRVUG8', '#PCJYUP2L8', '#22VJPRQRL', '#RJQ8JQ8QR', '#2CVR9C2U9', '#PG2UGPJP', '#L0QG9CG2U', '#9R0PR9Q0U', '#2G8VGQ208', '#8GJ8PGY0C', '#9QLLPJQ90', '#C9PGG8JC', '#8YG8RJV90', '#9YLLQLJGU', '#2GQQ2PU92', '#2PYU080Q', '#22QCRQCPG', '#C9JRU9U2', '#9JQLPGLJJ', '#8RR8QVR09', '#9QY2CLVJR', '#U0V0G2YY', '#28PR280CJ', '#P2RC2G9CL', '#9QVVY2P8', '#CVUGYPCP', '#9PVYQP080', '#29P2V8GLJ', '#YUJ88YRU', '#2RU0UGCUU', '#Y08LY8GJY', '#9R00QQU20', '#P08UJ920', '#2C00L02RU', '#YYQP9JGVC', '#YLULUC8L']

for idx, val in enumerate(war_cla_members):
    exec(f'membro_{idx} = Membros("{val}")')

for idx, _ in enumerate(war_cla_members):
    if current_river_race['clan']['participants'][idx]['tag'] in war_cla_members: #current_river_race is the return of the clashroyale api request. (https://developer.clashroyale.com/#/documentation)
        membro_1.pontos = current_river_race['clan']['participants'][idx]['decksUsedToday']
Davi A. Sampaio
  • 374
  • 2
  • 12
  • 1
    Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Jan 21 '22 at 16:54
  • 2
    "I used the exec() function and the for loop to create various class objects and store them in variables"—this is generally a bad idea and I strongly urge you not do do it. See [How do I create variable variables?](https://stackoverflow.com/q/1373164/354577) TL;DR: use dictionaries, lists, and other data structures instead. In this case you might start with `membros = []`, for example. – ChrisGPT was on strike Jan 21 '22 at 16:55
  • 1
    can you please post also the code itself, so we can help more? – XxJames07- Jan 21 '22 at 16:56
  • How many entries are in `war_cla_members`? If it's fewer than two you _won't_ have a `membros_1`. With zero entries, no `membros_X` will be created. With one entry, you'll get `membros_0`, and so on. – ChrisGPT was on strike Jan 21 '22 at 16:58
  • @Chris, the len() of `war_cla_members` is 47. also on the left of the image, you can see that `membro_1` was defined. the same error happens when i try to access the value of `membros_0` – Davi A. Sampaio Jan 21 '22 at 17:36
  • @XxJames07- , of course. i edited the post. – Davi A. Sampaio Jan 21 '22 at 17:37
  • @Chris, thanks for the tip; I will try to adapt my code using lists. But it will still be good to know why this error is occurring in the current case – Davi A. Sampaio Jan 21 '22 at 17:43
  • I suspect this is your IDE being unable to *statically* determine that `membro_1` is defined. If you actually run your code, there should be no problem. – chepner Jan 21 '22 at 18:01

0 Answers0