OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

NameError: name 'x2' is not defined [duplicate]

  • Thread starter Thread starter James Woods
  • Start date Start date
J

James Woods

Guest
I am trying to read data from a senor using my PI and in order to calculate the magnitude to determine acceleration but but I always get the following error when I run a test program

Code:
Time elasped: 0
Traceback (most recent call last):
  File "MMA7455.py", line 47, in <module>
    print("X = ", x2)
NameError: name 'x2' is not defined

This is the the code that ive been using

Code:
def calculateMag():
   x = MMA7455.getValueX()
   x2 = ((x + 128) % 256) -128

   y = MMA7455.getValueY()
   y2 = ((y - 240 + 128) % 256) -128

   z = MMA7455.getValueZ()
   z2 = ((z - 64 + 128) % 256) -128

   magnitude = int(math.sqrt((x2*x2) + (y2*y2) + (z2*z2)))

return x2, y2, z2, magnitude

for i in range (1000):
   timeGo()
   calculateMag()
   print("X = ", x2)
   print("Y = ", y2)
   print("Z = ", z2)

Ive tried passing x, y, z into the function but this didnt seem to work.Thanks for the help it would be much appreciated.
<p>I am trying to read data from a senor using my PI and in order to calculate the magnitude to determine acceleration but but I always get the following error when I run a test program</p>

<pre><code>Time elasped: 0
Traceback (most recent call last):
File "MMA7455.py", line 47, in <module>
print("X = ", x2)
NameError: name 'x2' is not defined
</code></pre>

<p>This is the the code that ive been using </p>

<pre><code>def calculateMag():
x = MMA7455.getValueX()
x2 = ((x + 128) % 256) -128

y = MMA7455.getValueY()
y2 = ((y - 240 + 128) % 256) -128

z = MMA7455.getValueZ()
z2 = ((z - 64 + 128) % 256) -128

magnitude = int(math.sqrt((x2*x2) + (y2*y2) + (z2*z2)))

return x2, y2, z2, magnitude

for i in range (1000):
timeGo()
calculateMag()
print("X = ", x2)
print("Y = ", y2)
print("Z = ", z2)
</code></pre>

<p>Ive tried passing x, y, z into the function but this didnt seem to work.Thanks for the help it would be much appreciated.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top