Reputation: 145
[EDIT FOR FUTURE READERS] In this post, I bring up the term "vector" a lot. When I said vector, I meant a vector in physics and at the time did not know that vector is also a datatype in programming.
A little piece of my code is here:
x_value = item[0]
y_value = item[1]
x_total += x_value
y_total += y_value
The error I'm getting is this:
File "main.py", line 65, in <module>
get_best();
File "main.py", line 51, in get_best
x_total += x_value
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
sh-4.3$
And I just don't understand why this is not okay.
Here is all of my code (It might not be great, but I'm a true beginner and am just lost pretty much all of the time):
# Program takes all vectors, puts them in a list, iterates through every possible combination,
# And selects the one with the result closest to 0 aka starting point.
import math;
import itertools;
vectors = [];
temp_list = [];
best_vector = [];
A = [11.5, 30];
B = [9.2, 55];
C = [8.7, 125];
D = [12.9, 190];
E = [5.5, 340];
F = [8.0, 295];
G = [4.2, 140];
H = [10.0, 315];
I = [14.3, 220];
J = [5.6, 70];
K = [7.5, 160];
L = [3.6, 255];
M = [7.1, 200];
N = [5.9, 15];
O = [6.4, 285];
P = [9.9, 35];
Q = [12.1, 135];
R = [11.0, 115];
S = [5.8, 245];
T = [9.4, 300];
#Just get them in a list to iterate them initially
all_vectors = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T];
for vector in all_vectors:
x_component = vector[0]*(math.cos(vector[1]*(180/math.pi)));
y_component = vector[0]*(math.sin(vector[1]*(180/math.pi)));
vector = [x_component, y_component];
vectors.append(vector);
#Vector list with components.
best = 100;
options = [itertools.combinations(vectors,10)];
def get_best ():
x_total = 0;
y_total = 0;
x_value = 0;
y_value = 0;
result = 0;
for item in temp_list:
x_value = item[0]
y_value = item[1]
x_total += x_value
y_total += y_value
result = sqrt(((x_total)^2)+((y_total)^2))
if result < best:
best = result
best_vector.append(result + "meters off")
for item in temp_list:
best_vector.append(item);
#Loop through all possible options, roughly 170,000 options.
for opt in options:
for char in opt:
temp_list.append(char);
get_best();
print best_vector;
Upvotes: 0
Views: 1175
Reputation: 2060
When you print out x_value
this is what your list has [-10.490162792663932, -4.71237568360262]
.
To fix that error you would need to do something like this I believe ( Since I dont know what output you are looking for, this is just a fix to the error)
x_total += x_value[0]
y_total += y_value[1]
That being said. Your entire code has alot more error which I fixed. I have posted the code below but not sure about what output you are looking for
import math;
import itertools;
vectors = [];
temp_list = [];
best_vector = [];
A = [11.5, 30];
B = [9.2, 55];
C = [8.7, 125];
D = [12.9, 190];
E = [5.5, 340];
F = [8.0, 295];
G = [4.2, 140];
H = [10.0, 315];
I = [14.3, 220];
J = [5.6, 70];
K = [7.5, 160];
L = [3.6, 255];
M = [7.1, 200];
N = [5.9, 15];
O = [6.4, 285];
P = [9.9, 35];
Q = [12.1, 135];
R = [11.0, 115];
S = [5.8, 245];
T = [9.4, 300];
#Just get them in a list to iterate them initially
all_vectors = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T];
for vector in all_vectors:
x_component = vector[0]*(math.cos(vector[1]*(180/math.pi)));
y_component = vector[0]*(math.sin(vector[1]*(180/math.pi)));
vector = [x_component, y_component];
vectors.append(vector);
#Vector list with components.
options = [itertools.combinations(vectors,10)];
def get_best ():
best = 100;
x_total = 0;
y_total = 0;
x_value = 0;
y_value = 0;
result = 0;
print(temp_list)
for item in temp_list:
x_value = item[0]
y_value = item[1]
x_total += x_value[0]
y_total += y_value[1]
result = math.sqrt(((x_total)**2)+((y_total)**2))
if result < best:
best = result
best_vector.append(result)
for item in temp_list:
best_vector.append(item);
#Loop through all possible options, roughly 170,000 options.
for opt in options:
for char in opt:
temp_list.append(char);
get_best();
print(best_vector)
Upvotes: 1