I am trying to display a menu for a group of routers that is described by a dictionary that contains pieces of information about their status. To do this, I have a function that takes the dictionary routers
that contain the device information as input. The dictionary is converted into a list of dictionary objects, called router_list
, sorted by the value of device name, with the use of a for-loop. All the devices are saved into the menu_text
variable, which in turn is used to enumerate and print the devices.
When I run this code I get this error:
python3 menu.py
Traceback (most recent call last):
File "menu.py", line 28, in <module>
'password': '2020'
File "menu.py", line 6, in show_menu
router_list.append("%s. %s " % ((i+2), value['name'],))
TypeError: string indices must be integers
Here's my code:
def show_menu(routers):
router_list = []
i = 0
for key, value in enumerate(routers):
router_list.append("%s. %s " % ((i+2), value['name'],))
i = i + 1
menu_text = '\n'.join(router_list)
print(("""
---------------------------------------------------------
-----------------------------------------------------
0.EXIT
1.CONFIG ALL
"""+ menu_text +"""
---------------------------------------------------------
"""))
if __name__ == '__main__':
show_menu({
'name': 'cisco_ios',
'ip': '192.168.122.217',
'username': 'admin',
'password': '2020'
})