如何修复'Type Error:+的不支持的操作数类型:'int'和'list''

时间:2019-07-09 01:17:50

标签: python-3.x

from random import choice


class RandomWalk():

     """a class generates random datas"""
    def __init__(self, num_points=5000):

        self.num_points = num_points
        self.x_values = [0]
        self.y_values = [0]


    def fill_walk(self):
        """caculate all points generated by RandomWalk """
        while len(self.x_values) < self.num_points:
            #decide the direction and distance
            x_direction = choice([1,-1])
            x_distance = choice([0, 1, 2, 3, 4])
            x_step = x_direction * x_distance
            #the same with x
            y_direction = choice([1, -1])
            y_distance = ([0, 1, 2, 3, 4])
            y_step = y_direction * y_distance

            if x_step == 0 and y_step ==0:
                continue

            #caculate next_x and next_y    
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step



            self.x_values.append(next_x)
            self.y_values.append(next_y)


rw_visual.py

这是代码

import matplotlib.pyplot as plt
from random_walk import RandomWalk
#generate a instance named RandomWalk, paint all included points
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s=15)
plt.show()

The result showed that random_walk.py", line 26, in fill_walk
    next_y = self.y_values[-1] + y_step
TypeError: unsupported operand type(s) for +: 'int' and 'list'`

1 个答案:

答案 0 :(得分:0)

当您绑定到next_x = self.x_values[-1] + x_step时,self.x_values[-1]的类型为int,而x_step的类型为列表。

您忽略了choice,因此程序将y_distance视为列表。 在python中,将int与列表相乘是完全合法的, 因此您最终会以y_step[0*y_distance, 1*y_distance, 2*y_distance, 3*y_distance, 4*y_distance]。 因此,y_step在您添加列表时属于列表类型。

#decide the direction and distance
x_direction = choice([1,-1])
x_distance = choice([0, 1, 2, 3, 4])
x_step = x_direction * x_distance
#the same with y
y_direction = choice([1, -1])
y_distance = ([0, 1, 2, 3, 4]) # Here you left out choice
y_step = y_direction * y_distance

因此,当程序遇到错误时,它正在尝试

next_y = self.y_values[-1] + [0*y_distance, 1*y_distance, 2*y_distance, 3*y_distance, 4*y_distance]

这是导致错误的原因,因为您无法在Python中将整数添加到列表中。这说明了您的错误。

该类的新代码:

from random import choice 

class RandomWalk():
    """a class that generates random data"""
    def __init__(self, num_points=5000):
        self.num_points = num_points
        self.x_values = [0]
        self.y_values = [0]

    def fill_walk(self):
        """calculate all points generated by RandomWalk """
        while len(self.x_values) < self.num_points:
            #decide the direction and distance
            x_direction = choice([1,-1])
            x_distance = choice([0, 1, 2, 3, 4])
            x_step = x_direction * x_distance
            #the same with y
            y_direction = choice([1, -1])
            y_distance = ([0, 1, 2, 3, 4])
            y_step = y_direction * y_distance

            if x_step == 0 and y_step ==0:
                continue

            #calculate next_x and next_y    
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step

            self.x_values.append(next_x)
            self.y_values.append(next_y)

如果您想打动人,

数据可以是复数或单数。 数据的单数是基准;总是单数。 例如,“其中有多少数据?先生,仅返回了一个数据。”

Source