+ =:'int'和'list'不支持的操作数类型

时间:2015-08-12 22:15:43

标签: python typeerror

[编辑未来读者]在这篇文章中,我提出了很多“矢量”一词。当我说矢量时,我的意思是物理学中的矢量,当时并不知道矢量也是编程中的数据类型。

我的代码中有一小部分在这里:

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$

我得到的错误是:

# 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;

我只是不明白为什么这不好。

这是我的所有代码(可能不是很好,但我是一个真正的初学者,而且我几乎一直都失去了):

{{1}}

1 个答案:

答案 0 :(得分:1)

当您打印x_value时,这就是您的列表[-10.490162792663932, -4.71237568360262]

要解决这个错误你需要做这样的事情我相信(因为我不知道你在寻找什么输出,这只是对错误的修复)

    x_total += x_value[0]
    y_total += y_value[1]

话虽如此。您的整个代码有很多错误我修复了。我已经发布了以下代码,但不确定您要查找的输出

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)