0

I need to define multiple variables within a while loop to be called inside of the while loop in Python.

Code:

tau = 0

while tau < 10:

(tau)

   d_abrv = "d" % tau
   day = "day" % tau

d_abrv = datetime.now() + timedelta(days=tau)
day = d_abrv.strftime('%a %d-%b-%y')
image_date = d_abrv.strftime('%Y%m%d')

plt.savefig(homedir + "/out/_tmax_" + d_abrv + "_ne.png", dpi = 300)

tau = tau + 1

sys.exit()

What am I doing wrong?

Ideally, I need to define a variable that loops through number 0 through 10. After, I need to be able to use this defined variable throughout the script, including defining new variables with the variable, e.g., tau=0, change day0 to day=day+tau

12
  • (tau)? What do you expect this to do? Commented Oct 7, 2015 at 13:01
  • I'm not quite sure... I found a python script that appeared to achieve what I am trying to do so, trying to hack my way through the code. Please forgive my obviously-limited knowledge of Python! Commented Oct 7, 2015 at 13:02
  • I guess you got an IndentationError? Commented Oct 7, 2015 at 13:03
  • 1
    could you add a description of what isn't working? Commented Oct 7, 2015 at 13:04
  • 1
    Aaron Perry, could you edit the indentation in your question, people will just keep on commenting on that and I think we clarified that this isn't your problem. I tried to submit an edit but apparently adding blank spaces doesn't qualify as a valid edit... Commented Oct 7, 2015 at 13:16

2 Answers 2

3

Not entirely sure what you mean, but would this suffice?

from datetime import datetime
from datetime import timedelta


for tau in range(0,10):
   d_abrv = "d" + str(tau)
   day = "day" +str(tau)
   d_abrv = (datetime.now() + timedelta(days=tau))
   day = d_abrv.strftime('%a %d-%b-%y')
   image_date = d_abrv.strftime('%Y%m%d')
   plt.savefig(homedir + "/out/_tmax_" + d_abrv + "_ne.png", dpi = 300)

sys.exit()

although I must say I'm not sure what this means:

plt.savefig(homedir + "/out/_tmax_" + d_abrv + "_ne.png", dpi = 300)
Sign up to request clarification or add additional context in comments.

7 Comments

you should change it to a for loop since you know how many times to loop.
@scrineym he's saving the plot into that directory with the provided quality (dpi). It's a matplotlib function savefig()
This is great, except that it's not processing any plots, except plot 9... It's skipping over the processing part with basemap, etc etc
@AaronPerry I'm wondering are you doing your processing outside of the for loop ? If you are tau would stay at 9 because that was the last value it was assigned
How can I check to see if the processing is running outside of the for loop?
|
-1

If I correctly understood your problem, you want to create a new variable in every iteration of the loop. I have done this successfully in the past by using exec(). I am still uncertain whether this is a pythonic thing to do, but it worked. Based on your previous comments, I would suggest the following code:

tau = 0
timeformat = "'%a %d-%b-%y'"
timeformat2 = "'%Y%m%d'"

while tau < 10:

  exec("d%d = datetime.now() + timedelta(days=tau)" % tau)
  exec("day%d = d%d.strftime(%s)" % (tau,tau,timeformat))
  exec("image_date = d%d.strftime(%s)" % (tau,timeformat2))

  exec("plt.savefig(homedir + '/out/_tmax_' + day%d + '_ne.png', dpi = 300)" % tau)

  tau = tau + 1

sys.exit()

I tested this code (on some dummy data i plotted) and I successfully get the plots saved with filenames "_tmax_Fri 09-Oct-15_ne.png" etc.

The rest of your code would have the variables day0, day1, etc. available for use.

2 Comments

It's certainly not „pythonic“ and definately not the way to go. You need exec() to define the variables and exec() again to refer to them, so you are creating, compiling, and executing extra code instead of simply storing the values in a dictionary. Or even simpler in a list.
did you apply correct indentation to the "tau = tau + 1" line? Because that would cause an infinite loop. I am still wondering if you use indentation correctly. Incorrect indentation could also have caused your problem that you mentioned in the other comment: if your plt.savefig(...) is not indented to be inside the loop, but outside, it would only be called once after the final iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.