[Python] Trying to use enumarate to list my folders as a menu, however its not finding the...

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    apologies this one is going to be super trivial but I am tired and can't see the error I am making.

    So I am using enumerate to grab a list of all my sub-folders inside a folder I have and display it as a menu for the user to choose one. However when I try to get a value from the user, I keep stuck inside the loop as the option is not recognized as one of the keys.

    This is my code:

    import os

    path = "/media/user/msu"

    subfolders = [f.name for f in os.scandir(path) if f.is_dir()]

    #print(subfolders)

    menu = {index: value for index, value in enumerate(subfolders, start=1)}

    selection = 0

    while selection == 0:

    options = menu.keys()

    for entry in options:

    print(entry, menu[entry])


    selection = input("Please Select:")

    if selection in menu.keys():

    break

    else:

    print("Unknown Option Selected!")
    selection = 0


    everything runs fine until I ask for user input. After that no matter what option chosen, I get "Unknown Option Selected" printed and the loop continues

    Can anyone help? (sorry if this is a basic mistake)

    Continue reading...

Compartilhe esta Página