I am pretty new to Python and am trying to use some code I found online for an undergrad physics project. This code includes the clifford library, which is causing my problems. Given the syntax of the code and the fact that I installed it using pip3, it should be Python 3.
After running this code:
from __future__ import division
import numpy
from clifford import *
layout, blades = Cl(3,0)
e0, e1, e2 = [blades['e%i'%k] for k in range(3)]
I = (e0^e1^e2)
I get the following traceback:
Traceback (most recent call last):
File "/Users/melissa/Documents/Fodje.py", line 5, in <module>
e0, e1, e2 = [blades['e%i'%k] for k in range(3)]
File "/Users/melissa/Documents/Fodje.py", line 5, in <listcomp>
e0, e1, e2 = [blades['e%i'%k] for k in range(3)]
KeyError: 'e0'
I asked my CS professor and he said there is likely a problem with the indexing in blades
.
Can anyone help me in trying to troubleshoot this issue? Thanks!
The problem here is that the dict object blades
contains keys e1, e2, e3 and so on. The range()
function starts at 0, so range(3)
essentially returns the list [0,1,2]
- (its actually an ittarator not a list, but you don't need to worry about that).
The reason you are getting a key Error is, python is looking in blades for the key e0 and it doesn't exist. replace k with k+1 and it should work.
e.g.e1, e2, e3 = [blades['e%i'%(k+1)] for k in range(3)]
In general if your trying to troubleshoot yourself, a key Error means your trying to look up something that isn't there. So a good idea might be to print that object. So to diagnose what was wrong here I just did print(blades)
and it became clear. I hope this helps.
The full code would be:
from __future__ import division
import numpy
from clifford import *
layout, blades = Cl(3,0)
e1, e2, e3 = [blades['e%i'%(k+1)] for k in range(3)]
I = (e1^e2^e3)