简体   繁体   中英

How to iterate a loop based on a user's input?

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)

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM