Sam Vruggink Sam Vruggink - 2 months ago 8
Python Question

Count and sort items in dictionaries

When the user gives input it puts it in a dictionary. When nothing or blanc is filled in it code should stop and give all the inputs + occurences in the dictionary.

nummer = 1
def namen(namen):
var = 1

key = {}

while var == 1:
invoer = input('Volgende naam: ')
if invoer != '':
global nummer
key[nummer] = invoer
nummer +=1
else:

return key
break

hey = (namen(5))


I tried
counter
and a
for
loop but that doesn't work.
So for instance if input = `h, d, d, hh, a, s

`it should give `
h=1
d=2
hh=1
a=1
s=1`

Answer

This looks working..

key = {}

while True:
    uinp = raw_input('Write something: ')
    print uinp
    if len(uinp) == 0:
        break

    try:
        key[uinp] += 1
    except:
        key[uinp] = 1

print key

is this what you wanted?