Search code examples
pythonlistpython-2.7spliteasygui

How do I create a list, then len() it, then make an entry for every 'section' in that list in Python?


So, I'm working on a project, which requires me to find how many 'objects' are in a list, which I accomplished via len(), however, now I need to make it where there is a choice (in easygui's choicebox()) for every entry in that list, without raising an 'out of range' exception.

Basically, if there are 3 entries in the list, then I need choicebox(msg="",title="",choices=[e[1]) to become choicebox(msg="",title="",choices=[e[1],e[2],e[3]]), and if there are 5 choices, I need it to become choicebox(msg="",title="",choices=[e[1],e[2],e[3],e[4],e[5]])and so on.

Notes: I need e[0] to be skipped, that being either .DS_Store, desktop.ini, or thumbs.db.
I'm listing directories just before that, so if you could tell me how to only make the directories end up on the list, or even how to limit the entries to 22, that'd be greatly appreciated as well!!

Sorry for the noobish question! I couldn't think of how to search for something like this, or a suiting title even.....

EDIT: Here's my script due to request. It is almost bugless, but very incomplete and broken;

#imports
from easygui import *
import os
#variables
storyname = None
#get user action
def selectaction():
    d = str(buttonbox(msg="What would you to do?",title="Please Select an Action.",choices=["View Program Info","Start Reading!","Exit"]))
    if d == "View Program Info":
        msgbox(msg="This program was made solely by Thecheater887. Program Version 1.0.0.               Many thanks to the following Story Authors;                                                Thecheater887 (Cheet)",title="About",ok_button="Oh.")
        selectaction()
    elif d == "Exit":
        exit
    else:
        enterage()
#get reader age
def enterage():
    c = os.getcwd()
#   print c
    b = str(enterbox(msg="Please enter your age",title="Please enter your age",default="Age",strip=True))
#   print str(b)
    if b == "None":
        exit()
    elif b == "Age":
        msgbox(msg="No. Enter your age. Not 'Age'...",title="Let's try that again...",ok_button="Fine...")
        enterage()
    elif b == "13":
#       print "13"
        choosetk()
    elif b >= "100":
        msgbox(msg="Please enter a valid age between 0 and 100.",title="Invalid Age!")
        enterage()
    elif b >= "14":
#       print ">12"
        choosema()
    elif b <= "12":
#       print "<12"
        choosek()
    else:
        fatalerror()
#choose a kids' story
def choosek():
    os.chdir("./Desktop/Stories/Kid")
    f = str(os.getlogin())
    g = "/Users/"
    h = "/Desktop/Stories/Kid"
    i = g+f+h
    e = os.listdir(i)
    names = [name for name in e if name not in ('.DS_Store', 'desktop.ini', 'thumbs.db')]
    limit = 22 # maximum entries in the choicebox --> e[1] until e[22]
    for i in xrange(1, len(e)): # starting from 1 because you don't want e[0] in there
        if(i > limit):
            break # so if you have 100 files, it will only list the first 22
        else:
            names.append(e[i])
        #names = e[1:23]
    choicebox(msg="Please select a story.",title="Please Select a Story",choices=names)
#choose a mature story
def choosema():
    os.chdir("./Desktop/Stories/Mature")
#choose a teen's story
def choosetk():
    os.chdir("./Desktop/Stories/Teen")
def fatalerror():
    msgbox(msg="A fatal error has occured. The program must now exit.",title="Fatal Error!",ok_button="Terminate Program")
#select a kids' story
def noneavailable():
    msgbox(msg="No stories are available at this time. Please check back later!",title="No Stories Available",ok_button="Return to Menu")
    enterage()
selectaction()

Solution

  • So here is my solution (now that I have the code):

    def choosek():
        os.chdir("./Desktop/Stories/Kid")
        f = str(os.getlogin())
        g = "/Users/"
        h = "/Desktop/Stories/Kid"
        i = g+f+h
        e = os.listdir(i)
        names = [] # the list with the file names
        limit = 22 # maximum entries in the choicebox --> e[1] until e[22]
        for i in xrange(1, len(e)): # starting from 1 because you don't want e[0] in there
            if(i > limit):
                break # so if you have 100 files, it will only list the first 22
            else:
                names.append(e[i])
        choicebox(msg="Please select a story.",title="Please Select a Story",choices=names)
    

    I hope this is what you were looking for.