# Why my “print” function is ptinting the triplle of values of variable “g” and “ag” instead of only 1 value for ag and 3 for ag? browsing

• posted
0

Why my “print” function is ptinting the triplle of values of variable “g” and “ag” instead of only 1 value for ag and 3 for ag?

When O run y code, I have this as the result:
72
(0.15401078760623932, -0.9237408638000488, -0.35069024562835693)
72
(0.15401078760623932, -0.9237408638000488, -0.35069024562835693)
72
(0.15401078760623932, -0.9237408638000488, -0.35069024562835693)

My code:

'''This script records your device's orientation (accelerometer data) for 5 seconds, and then renders a simple plot of the gravity vector, using matplotlib.'''
import numpy as np
import sys
import motion
import matplotlib.pyplot as plt
from time import sleep
import console
g=0;
ag=0;
data=0;
data_ag=0;

del g
del ag
del data
del data_ag
fs=100;
duration=5;
def main():
console.alert('Motion Plot', 'When you tap Continue, accelerometer (motion) data will be recorded for 5 seconds.', 'Continue')
sleep(0.2)
print('Capturing motion data...')
num_samples = fs*duration;
data = []
data_ag = []
for i in range(num_samples):
sleep(1/fs)
ag=motion.get_user_acceleration();
g = motion.get_gravity();
#a=motion.get_gravity()
#motion.get_attitude()
#motion.get_magnetic_field()

``````    data.append(g);
data_ag.append(ag);

print('Capture finished, plotting...')

x_values = [x/fs for x in range(num_samples)]
for i, color, label in zip(range(3), 'rgb', 'XYZ'):
plt.plot(x_values, [g[i] for g in data], color, label=label, lw=2)
szg=sys.getsizeof(g)
print(szg)
print(g)
plt.grid(True)
plt.xlabel('t')
plt.ylabel('G')
plt.gca().set_ylim([-1.0, 1.0])
plt.legend()
plt.show()
``````

if name == 'main':
main()

• posted
0

your dataset (the variable data) is an array

``````data = []
``````

that contains tuples, which is the variable g.

``````data = [()]
``````

This tuple then contains 3 variables.

``````data = [(5.67, 1.87, 3.93)]
``````

Running this code:

``````for g in data:
print(g)
``````

will print every tuple in the data array, which is (5.67, 1.87, 3.93).
If you want to print a single float (the name for a number with decimal places), just use this:

``````print(g[0])
``````

Replace the 0 with a 1 to print the second float in g, and 2 for the third float in g

• @Sarmet said:

for i, color, label in zip(range(3), 'rgb', 'XYZ'):
plt.plot(x_values, [g[i] for g in data], color, label=label, lw=2)
szg=sys.getsizeof(g)
print(szg)
print(g)

In the above code, you are printing things out within the for... loop, which has 3 iterations. So the information in szg and g, which doesn't change, is still being printed once per trip around the loop.

• posted
0

``````import numpy as np
import sys
import motion
import matplotlib.pyplot as plt
from time import sleep
import console
g=0;
ag=0;
data=0;
data_ag=0;

del g
del ag
del data
del data_ag
fs=100;
duration=5;
def main():
console.alert('Motion Plot', 'When you tap Continue, accelerometer (motion) data will be recorded for 5 seconds.', 'Continue')
sleep(0.2)
print('Capturing motion data...')
num_samples = fs*duration;
data = []
data_ag = []
for i in range(num_samples):
sleep(1/fs)
ag=motion.get_user_acceleration();
g = motion.get_gravity();
#a=motion.get_gravity()
#motion.get_attitude()
#motion.get_magnetic_field()
data.append(g);
data_ag.append(ag);

print('Capture finished, plotting...')

x_values = [x/fs for x in range(num_samples)]
for i, color, label in zip(range(3), 'rgb', 'XYZ'):
plt.plot(x_values, [g[i] for g in data], color, label=label, lw=2)
szg=sys.getsizeof(g)
print(szg)
print(g)
print(ag)
print(type(data[1]))
plt.grid(True)
plt.xlabel('t')
plt.ylabel('G')
plt.gca().set_ylim([-1.0, 1.0])
plt.legend()
plt.show()

main()
``````

Internal error.

Oops! Looks like something went wrong!