4

I'm new here, but look here often for help. Anyways, I am trying to use tkSimpleDialog.askinteger() to ask for how many files the user needs to read into the program. I want to read the files in based on the integer that the user inputs in a for loop. I would index the file names f[1] through f[n] for the file names. Any input would be greatly appreciated!

Please view below for an idea of what I am trying to get at:

def callback2():
    NumDates = tkSimpleDialog.askinteger("NDates", "How many dates are there?")
    for dates in NumDates:
        filename[dates] = tkFileDialog.askopenfilename() 
        dates = dates + 1
        filenameDates.append(filename)

1 Answer 1

7

Assuming NumDates is an integer, you're looking for the range function:

for dates in range(NumDates):
    ...

In python 2.x, you can use xrange instead. This doesn't create an intermediate list so many people prefer it. In python 3, xrange was renamed range and the former range function which returns a list was removed -- When the lists are small, I usually just use range for compatibility, but there exist tools (2to3) to take care of these details for you as well, so it really isn't a big deal either way.

aside

Also, as written, there really is no need for the dates = dates + 1 (which is better written as dates += 1 when necessary).

Sign up to request clarification or add additional context in comments.

1 Comment

@user1620716: dates should probably be just date since represents just a one in the in NumDates range, which according to PEP 8 -- [Style Guide for Python Code really ought to be written num_dates otherwise it looks like the name of a class.

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.