首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复'Type Error:unsuppored operand type(s) for +:'int‘and 'list’‘

如何修复'Type Error:unsuppored operand type(s) for +:'int‘and 'list’‘
EN

Stack Overflow用户
提问于 2019-07-09 09:17:50
回答 1查看 68关注 0票数 0
代码语言:javascript
复制
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

以下是代码

代码语言:javascript
复制
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'`
EN

回答 1

Stack Overflow用户

发布于 2019-07-09 12:27:07

当您绑定到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时,它的类型是list。

代码语言:javascript
复制
#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

因此,当程序遇到错误时,它试图

代码语言:javascript
复制
next_y = self.y_values[-1] + [0*y_distance, 1*y_distance, 2*y_distance, 3*y_distance, 4*y_distance]

这导致了一个错误,因为您不能在Python中将整数添加到列表中。这就解释了你的错误。

类的新代码:

代码语言:javascript
复制
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)

另外,如果你想给人留下深刻印象:

数据可以是复数,也可以是单数。数据的单数是datum;总是单数。例如,“有多少数据?先生,只返回一个数据。”

Source

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56944224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档