Python Forum
TypeError: unhashable type: 'list'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: unhashable type: 'list'
#1
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.
Reply
#2
You never run votes!
Reply
#3
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
Reply
#4
You still had to call your function (votes)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unhashable error - Histogram code lsbpython 1 999 Aug-07-2022, 04:02 PM
Last Post: Yoriz
  search a list or tuple for a specific type ot class Skaperen 8 1,949 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,336 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,181 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,898 May-07-2022, 08:07 AM
Last Post: menator01
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,591 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,016 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,418 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,400 Jan-30-2021, 07:11 AM
Last Post: alloydog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020