0

I am having a hard time solving what I think SHOULD be an easy problem. I have a dictionary that consists of a key(interger that represents an option #) and a value (string that is a description of the option). I need to print each key value pair on a separate line inside of the input function.

file_codes = {2: 'iosFile',
              3: 'startup',
              4: 'running'}
host_ip = '10.10.10.10'
users_numeric_entry = int(input(f'Please pick a configuration to copy for: {host_ip}\n))

Input prompt should be like below:

Please pick a configuration to copy for: 10.10.10.10

2 - iosFile
3 - startup
4 - running
: 

I feel that I need a to create a for loop to iterate through the dictionary, but I am having a hard time coming up with the code to achieve my desired output.

adamz88
  • 319
  • 3
  • 12

4 Answers4

2

I would print things out first, and then prompt with just :

print(f'Please pick a configuration to copy for: {host_ip}\n')
for k, v in file_codes.items():
    print(str(k) + ' - ' + v)
users_numeric_entry = int(input(':'))

If you are using python 2.x then dictionaries are not ordered and there is no guarantee it will print in the order you expect. In python 3.6+ they are ordered.

Zachary Oldham
  • 838
  • 1
  • 5
  • 21
2

Something like this:

file_codes = {2: 'iosFile',
              3: 'startup',
              4: 'running'}
host_ip = '10.10.10.10'
menu = '\n'.join(['{} - {}'.format(k, v) for k, v in file_codes.items()])
users_numeric_entry = int(input(f'Please pick a configuration to copy for: {host_ip}\n {menu}'))
balderman
  • 22,927
  • 7
  • 34
  • 52
2

I'm a huge advocate of the string formatting in Python. I would recommend looking into the abilities of the ''.format() options.

for k, v in sorted(file_codes.items()):
      print('{} - {}'.format(k, v))
pmackni
  • 307
  • 3
  • 12
  • Personally, I prefer the list comprehension of balderman's solution to mine, but any of these should do the trick. I prefer the ''.format method for some of the added options in formatting the string beyond just printing a variable. – pmackni Jul 17 '19 at 15:51
  • most (if not all) functionalities of `format` exist in `f-strings` – Tomerikoo Jul 17 '19 at 15:57
  • 1
    It's just most, not all. I looked into it extensively a while ago for a script I was working on and found better controls existed in the `format` options that don't have an `f-strings` equivalent. For general use, those extra controls could be considered niche and most formatting can be accomplished with either. I tend to have more niche needs when I script, so I typically prefer `format`. – pmackni Jul 17 '19 at 16:23
  • 1
    I'd like to alter my previous viewpoint a little. `f-strings` do offer a few added controls that over `format` as well as a performance boost, so `f-strings` are slightly better in general, though it's probably more a question of preference unless you're covering such a large amount of text that it actually creates a performance impact. – pmackni Jul 17 '19 at 16:43
  • 1
    Agree with what you say. I personnally am too still stuck with `format` as I believe will take me time to get used-to `f-s` and also lazy to change in all my project :) Just pointed out in my comment for future reference for readers – Tomerikoo Jul 17 '19 at 16:48
  • I missed this point when I previously looked into them, but their ability to contain any valid python is pretty nice if for some reason you need to call a method in the middle of a string. Maybe one day we'll catch up. :) – pmackni Jul 17 '19 at 16:51
-2
In [34]: file_codes = {2: 'iosFile', 
    ...:               3: 'startup', 
    ...:               4: 'running'}                                                                                                                                                                                                                                                                                          

In [35]: for k,v in sorted(file_codes.items(), key=operator.itemgetter(0)): print(k, '-', v)                                                                                                                                                                                                                                  
2 - iosFile
3 - startup
4 - running
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241