Python Forum
I need help with loop one function and creating variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with loop one function and creating variables
#1
my code that is working
*****************************************************
names = ['xx','yy','cc']

for name in names:
    exec("%s_mar = []" % name)
    some code...
    else:
       pass

xx_var = countAll(xx_mar)
yy_var = countAll(yy_mar)
cc_var = countAll(cc_mar)
***********************************************************
I need help with loop one function and creating variables - code below doesn't work

for name in names:
    eval("%s_var)" % name) = countAll('{0}_mar'.format(name))
    else:
       pass
Reply
#2
http://stupidpythonideas.blogspot.com/20...reate.html
don't create names dynamically, use dict instead
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
thx for replay
also need help with this

time = countAll(x_mar)
#this works
name = 'x'
time = countAll('{0}_mar'.format(name))
#this doesn't work
Reply
#4
You are not listening and keep trying to create variables names dynamically. Almost always and certainly in this case it's a bad idea.
Please put your question in broader context and elaborate what your goal is, so we can understand and try to help you better
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print variables in function? samuelbachorik 3 852 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  User-defined function to reset variables? Mark17 3 1,592 May-25-2022, 07:22 PM
Last Post: Gribouillis
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  how to use 3 variables python loop evilcode1 2 1,632 Nov-12-2021, 11:43 AM
Last Post: jamesaarr
  creating a loop yk303 2 1,834 Feb-08-2021, 08:41 PM
Last Post: nilamo
  Do I have to pass 85 variables to function? Milfredo 10 4,189 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Exit Function - loop Tetsuo30 2 2,025 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30
  Creating a variables inside FOR loop zazas321 5 4,040 Sep-16-2020, 04:42 PM
Last Post: Naheed
  [split] Creating a variable as a function DPaul 23 6,576 Sep-07-2020, 05:20 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020