0

I have this code below to show a list of options that user can choose entering a number.

It is working fine but I don't understand why the options don't appear ordered.

Instead of having:

1 - User Management
2 - Uploads
8 - Exit

I'm having:

1 - User Management
8 - Exit
2 - Uploads

Do you see where the issue is?

Choice = namedtuple("Choice", ['msg', 'callback'])

def nav():
    print ""
    while True:
        response_options = {'1': Choice(msg="User Management", callback=userManagment),
                            '2': Choice(msg="Uploads", callback=upload),
                            '8': Choice(msg="Exit", callback=sys.exit)}
        result = make_choice(response_options)
        if result is None:
            print "-> Selected option not available."
            print ""
        else:
            result.callback()
    return False

def make_choice(optiontable):
    for resp, choiceobj in optiontable.items():
        print("{} - {}".format(resp, choiceobj.msg))
    print ""
    print "Select an option: "
    print ""
    usr_resp = raw_input(">> ")
    print ""
    return optiontable.get(usr_resp, None)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
techman
  • 423
  • 1
  • 7
  • 17

1 Answers1

3

Standard python dictionaries are unordered. See this question for sorting a dictionary by key: How can I sort a dictionary by key?

Community
  • 1
  • 1
Eric Appelt
  • 2,843
  • 15
  • 20