python - TypeError: unhashable type: 'list' (a few frames into program) -


so first project in python outside of code academy, decided make basic molecular dynamics simulator on pygame. works fine while, electrons start move fast, , strip other electrons off atoms, typeerror in title. have no idea comes from, , appears after program has been running long enough me mess physics.

now know error telling me i'm trying pass list somewhere shouldn't, i've looked on program , can't figure out where. error pops in bit tells electrons how orbit atom angle = finda(particles[el], particles[nuc]) + 0.001, controlled block of code near end tells program in order physics, , list of each electron meant orbit controlled point, , on.

so decided give code.

import sys, pygame, math pygame.locals import *  pygame.init() sizescreen = width, height = 1000, 700 sizemenu = width, height = 652, 700   e = 1.6 * 10 ** -19 particles = {} mx, = 0, 0 selected = []  def findorbital(el):     in particles:         if != el , particles[a][4] != 'el':             if findd(particles[el], particles[a]) < 5 * 10 ** -11 , pti[particles[a][4]][7] > len(particles[a][5]):                 particles[a][5].append(el)                 particles[el][5].append(a)    def searcher(list, item):     in list:         if == item:             return true     return false  def moveatels(el, nuc):     angle = finda(particles[el], particles[nuc]) + 0.001     particles[el][0] = particles[nuc][0] + 50 * math.cos(angle)     particles[el][1] = particles[nuc][1] + 50 * math.sin(angle)   def check(each):     if particles[each][0] < 175:         particles[each][2] = -particles[each][2]         particles[each][0] = 175     elif particles[each][0] > 1000:         particles[each][2] = -particles[each][2]         particles[each][0] = 1000     if particles[each][1] < 0:         particles[each][3] = -particles[each][3]         particles[each][1] = 0     elif particles[each][1] > 700:         particles[each][3] = -particles[each][3]         particles[each][1] = 700     if particles[each][4] == 'el':         = 'n'         findorbital(each)         if != 'n':             particles[each][5].append(a)             particles[a][5].append(each)   def findd(self, other):     return math.hypot((self[0] - other[0]), (self[1] - other[1])) * 0.62 * 10 ** -12 def finda(self, other):     return math.atan2((self[1] - other[1]), (self[0] - other[0]))   def move(self):     other in particles:         if particles[other] != self , self[5] != particles[other] [5] , not searcher(self[5], other):             d = findd(self, particles[other])             if d == 0:                 self[5].append(other)                 particles[other][5].append(self)                 break             angle = finda(self, particles[other])             f = 8987550000 * (pti[self[4]][4] * pti[particles[other][4]][4] * e ** 2)/d ** 2             = int(f/pti[self[4]][5])             ax = * math.cos(angle)             ay = * math.sin(angle)             self[2] += ax/(10 ** 16)             self[3] += ay/(10 ** 16)     self[0] += self[2]/(10 ** 8)     self[1] += self[3]/(10 ** 8)   pressed = '' press = {'katom':[2,148,2,32,0],'knuc':[2,148,36,66,0],'kel':[2,148,70,100,0]}  pti = {'el':[0, 0, 0, 0, -1, 9.11 * 10 ** -31, pygame.image.load("electron.png"), 2], 'hnuc' : [185, 214, 8, 37, 1, 1.7 * 10 ** -27, pygame.image.load("nuc/hnuc.png"), 2, 1], 'henuc': [586, 613, 8, 37, 2, 6.6 * 10 ** -27, pygame.image.load("nuc/henuc.png"), 2, 2], 'linuc': [185, 214, 40, 69, 1, 1.16 * 10 ** -26, pygame.image.load("nuc/linuc.png"), 8, 1], 'benuc': [216, 246, 40, 69, 2, 1.53 * 10 ** -26, pygame.image.load("nuc/benuc.png"), 8, 2], 'bnuc' : [428, 457, 40, 69, 3, 1.84 * 10 ** -26, pygame.image.load("nuc/bnuc.png"), 8, 3], 'cnuc' : [460, 489, 40, 69, 4, 2.04 * 10 ** -26, pygame.image.load("nuc/cnuc.png"), 8, 4], 'nnuc' : [492, 520, 40, 69, 5, 2.38 * 10 ** -26, pygame.image.load("nuc/nnuc.png"), 8, 5], 'onuc' : [523, 551, 40, 69, 6, 2.72 * 10 ** -26, pygame.image.load("nuc/onuc.png"), 8, 6], 'fnuc' : [554, 583, 40, 69, 7, 3.23 * 10 ** -26, pygame.image.load("nuc/fnuc.png"), 8, 7], 'nenuc': [586, 613, 40, 69, 8, 3.43 * 10 ** -26, pygame.image.load("nuc/nenuc.png"), 8, 8]}  menu = pygame.display.set_mode(sizemenu) screencolor = pygame.color(255, 255, 220) screen = pygame.display.set_mode(sizescreen) edgeobj = pygame.image.load("edge.png") addel = [pygame.image.load('addelectron1.png'), pygame.image.load('addelectron2.png')] addatom = [pygame.image.load("addatom1.png"), pygame.image.load("addatom2.png"), pygame.image.load("atomtable.png")] addnucleus = [pygame.image.load("addnuc1.png"), pygame.image.load("addnuc2.png"), pygame.image.load("nuctable.png")]  while true:     event in pygame.event.get():         if event.type == pygame.quit:             pygame.quit()             sys.exit(0)          elif event.type == mousemotion:             mx, = event.pos          mousestate = pygame.mouse.get_pressed()         if mousestate[0]:             key in press:                 if press[key][0] < mx <press[key][1] , press[key][2] < < press[key][3]:                     pressed = key                     press[key][4] = 1          if not mousestate[0] , pressed == 'kel':             particles[len(particles)] = [mx, my, 0, 0, 'el', []]             pressed = ''             press['kel'][4] = 0          if pressed != '':             if not mousestate[0]:                 if press[pressed][0] < mx <press[pressed][1] , press[pressed][2] < < press[pressed][3]:                     press[pressed][4] = 2                     pressed = ''          if press['knuc'][4] == 2 or press['katom'][4] == 2:             if  mousestate[0]:                 if 621 < mx < 651 , 2 < < 14:                     press['knuc'][4] = 0                     press['katom'][4] = 0                 if press['knuc'][4] == 2:                     nuc in pti:                         if pti[nuc][0] < mx < pti[nuc][1] , pti[nuc][2] < < pti[nuc][3]:                             press['knuc'][4] = 0                             selected.append(nuc)                 if press['katom'][4] == 2:                     nuc in pti:                         if pti[nuc][0] < mx < pti[nuc][1] , pti[nuc][2] < < pti[nuc][3]:                             = 0                             selected.append(nuc)                             while < pti[nuc][8]:                                 selected.append('el')                                 += 1                             press['katom'][4] = 0         if selected != []:             if not mousestate[0]:                 = len(particles)                 particles[a] = [mx, my, 0, 0, selected[0], [b b in range(a+1, len(selected)-1)]]                 item in selected:                     if item != selected[0]:                         particles[len(particles)] = [mx, my, 0, 0, item, [a]]                 selected = []       each in particles:         check(each)         move(particles[each])         check(each)         if len(particles[each][5]) > 0 , particles[each][4] == 'el':             moveatels(each, particles[each][5][0])         particles[each][5] = []      screen.fill(screencolor)     in particles:         screen.blit(pti[particles[a][4]][6], (particles[a][0] - 29, particles[a][1] - 31))     menu.blit(edgeobj, (0, 0))     menu.blit(addnucleus[press['knuc'][4]], (2, 2))     menu.blit(addatom[press['katom'][4]], (2, 2))     menu.blit(addel[press['kel'][4]], (2, 2))      pygame.display.flip() 

sorry if i'm being nuisance posting code, i'm complete n00b , i'm surprised got far without help. know whole thing untidy, if error i'd appreciate it.

next time i'll stick print("hello, world!")

so, believe happening when execute line angle = finda(particles[el], particles[nuc]) + 0.001, either el variable or nuc variable list of sort, rather single object.

this throws error because particles dict, , cannot have list or mutable type key in dict.

so, given error not execute immediately, suspect somewhere along line, in piece of code not executed, accidentally passing in list of sort.


if did mean things in particles dict using list key, should convert list tuple first: tuples lists, immutable, , can hashed , used key of dict.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -