-3

Write a function named "print_keys" that takes a key-value store as a parameter with strings as keys and integers as values. The function prints each key of the input on a separate line. That is, it prints all keys of the key-value store separated by new line characters

I've been stuck for a while on this problem and cant seem to get it. Ive tried making a for loop and concatenating with that but it isnt working how i expected it. Really need help with this, thanks.

this is what i have so far

def print_keys(dict):
    for key, value in sorted(dict.items()):
        print('{} - {}, {} - {}'.format(key, value[0], key, value[1]))

this is the error that shows error on input [{'spy': 9, 'stimulate': 8, 'strict': 8}]: 'int' object is not subscriptable

note the problem already has a list generated in their all i have to do is what the question is asking

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Btw, each value of your dictionary is a int, not a list with multiple items. – U13-Forward Nov 06 '18 at 01:52
  • And btw again, it's not good to name variables a builtin python keyword, that modifies the keyword to that variable's value, then the real keyword function is inaccessible. this case keyword is `dict`. – U13-Forward Nov 06 '18 at 01:54
  • @coderfanaticbsal replace your print statement with `print('{}: {}'.format(key, value))` the examine that output. Should be able to change a bit to get your desired output. Let us know if you're still stuck. – chickity china chinese chicken Nov 06 '18 at 02:04

1 Answers1

0

You code is assuming each dictionary item contains list with at-least two items - That's what the print with the value[0] and value[1] says to me. Yet the example input there is only a single integer per dictionary item. This is why you are receiving the "not subscriptable" error - the integer 9 is not subscriptable. If the entry for spy was [9 ,3], it would work fine.

Removing the index, and the next pair from the print is enough to make it work.

my_dict = {'spy': 9, 'stimulate': 8, 'strict': 8}

def print_keys(dict_to_print):
    for key, value in sorted(dict_to_print.items()):
        print('{} - {}'.format(key, value))

print_keys(my_dict)

EDIT: Sorted by the value

my_dict = {'band': -1, 'yard': 16, 'universe': 8, 'dock': -3, 'missionary': 2 }

def print_keys(dict_to_print):
    for key in sorted(dict_to_print, key=dict_to_print.get):
        value = dict_to_print[key]
        print('{} - {}'.format(key, value))

print_keys(my_dict)
Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • descriptor 'items' of 'dict' object needs an argument ( thats whats said when i try running it, sorry if im asking stupid questions i'm still really new to python) – coderfanaticbsal Nov 06 '18 at 02:38
  • @coderfanaticbsal - `dict` is a reserved word in python, although you can use it if you're careful. I edited the code above to remove its use. The above code worked before the change, so I assume your code does not defined the keyword `dict`, so python is going back to the reserved word. – Kingsley Nov 06 '18 at 02:46
  • Ah i see, so when i tried that this is the output it gave, function print_values incorrect on input [{'band': -1, 'yard': 16, 'universe': 8, 'dock': -3, 'missionary': 2}] printed: "band - -1 dock - -3 missionary - 2 universe - 8 yard - 16" expected: "-1 16 8 -3 2 ", so i think if i take one of the brackets out it will fix this issue. – coderfanaticbsal Nov 06 '18 at 02:50
  • Ok, you want to sort on `values`, not `items` - see https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value - but just using `in sorted(dict_to_print, key=dict_to_print.get)` should get you most of the way. – Kingsley Nov 06 '18 at 02:52
  • printed: "solid - -3 themselves - -3 cabinet - 0 hard - 3 museum - 6" expected: "6 -3 3 0 -3 " now its asking for the values so i updated it to the one you just edited, it seems that its taking the str and the int when it only wants the int for the key value pairs. @kingsley – coderfanaticbsal Nov 06 '18 at 03:01