Python Forum

Full Version: TypeError: unhashable type: 'list'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.
I'm trying to learn to code. I've done some simpler scripts in MEL and Python in Maya but I would like to learn more.

I'm stuck and I don't know what I've done wrong.
I got this first version to work:

election = {'Rocky':0, 'Rambo':0, 'Cobra':0, 'Dredd':0}
result = max(election)
while True:
    
    print 'Whom will you vote for?'
    print election
    name=raw_input('Write name here: ')

    if name in election:
        election[name] += 1
    else:
        print 'That name is not on the list!'
        election
But this second version will not work.
I get
Quote:TypeError: unhashable type: 'list'

and I have no idea what I'm doing wrong.
I know I have errors all through the code. I had to comment the last part out because I managed to make it not work at all. I don't remember what I removed. This was before the holidays. I've read what people have answered others that have gotten the same error as I did but I still have no idea how to fix it.

election = {'Rocky':0, 'Rambo':0, 'Cobra':0, 'Dredd':0}
def vote (election, name):
    if name in election:
        election[name] +=1
    print election

def votes(election, name):
    values = election.keys()  
    print name, 'has:',election.get(name, 0) ,'votes'

    print election[value]
    for value in values:
        print value
        
#def result(election):
#    flera = false
#    values = election.keys()
#    maxvalue = values[0]

#    for value in values[1:]:
#        if election[value] > election[maxvalue]:
#            maxvalue = value
#            many = false
#        print maxvalue

#        elif election[value] == election[maxvalue]:
#                many = true

#    if many:
#        print '* * * OPEN * * *'
#        else: print maxkey
I am a 3D artist by trade and I don't have that much time every week to sit down and learn but I took all day today and it does not feel like I've managed to get anything done at all. There is nothing in the code that works better than it did yesterday.

I would really appreciate it if someone had the knowledge and time to write a working version of this failed attempt of a voting script and maybe write some comments where I made the biggest errors.

I don't know if this is something programmers do. I'm just used to how it works on graphics forums where I help people learn how to create pixel art and 3d models by editing their images/models to show what the end result should/could look like and how to learn to do it.

If you need any 2D/3D graphics done then I could probably help you out.



TL;DR
I've dome simpler scripts for years but would like to learn to code.
Something went wrong and I have no idea why.
I need help to understand.
I can create graphics for you in return for help.
You never run votes!
It works now. One problem was how I ran the program.
I used brackets instead of parentheses around the name in the interpeter.
I had a few indentation errors and other minor issues too.

It is so much fun to learn Python and programming in general. :D
You still had to call your function (votes)