鱼C论坛

 找回密码
 立即注册
查看: 1318|回复: 7

[已解决]求助为什么list[each]不能加一个int,会报错

[复制链接]
发表于 2019-8-6 10:22:04 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random
class Fish:
  xmax=10
  ymax=10
  all=10
  step=1
  x=[]
  y=[]
  for each in range(all):#随机出生all个鱼。
    x.append([random.randint(0,xmax)])
    y.append([random.randint(0,ymax)])
  def move(self):
    for each in range(self.all):
      direction=random.randint(1,4)#1上2下3左4右
      if direction==1:
        self.y[each]+=self.step
      elif direction==2:
        self.y[each]-=self.step
      elif direction==3:
        self.x[each]-=self.step
      elif direction==4:
        self.x[each]+=self.step
      if self.x[each]<0:
        self.x[each]=-self.x[each]
      elif self.y[each]<0:
        self.y[each]=-self.y[each]
      elif self.x[each]>self.xmax:
        self.x[each]=self.x[each]-2*self.step
      elif self.y[each]>self.ymax:
        self.y[each]=self.y[each]-2*self.step
      for each in range(self.all):
        print('鱼%d的位置是(%d,%d)'%(each,self.x[each],self.y[each]))
f=Fish()
f.move()
最佳答案
2019-8-6 10:30:52
a1105089363 发表于 2019-8-6 10:28
就是这一段会错
if direction==1:
        self.y[each]+=self.step

  1. import random
  2. class Fish:
  3.   xmax=10
  4.   ymax=10
  5.   all=10
  6.   step=1
  7.   x=[]
  8.   y=[]
  9.   for each in range(all):#随机出生all个鱼。
  10.     x.append(random.randint(0,xmax))
  11.     y.append(random.randint(0,ymax))
  12.   def move(self):
  13.     for each in range(self.all):
  14.       direction=random.randint(1,4)#1上2下3左4右
  15.       if direction==1:
  16.         self.y[each]+=self.step
  17.       elif direction==2:
  18.         self.y[each]-=self.step
  19.       elif direction==3:
  20.         self.x[each]-=self.step
  21.       elif direction==4:
  22.         self.x[each]+=self.step
  23.       if self.x[each]<0:
  24.         self.x[each]=-self.x[each]
  25.       elif self.y[each]<0:
  26.         self.y[each]=-self.y[each]
  27.       elif self.x[each]>self.xmax:
  28.         self.x[each]=self.x[each]-2*self.step
  29.       elif self.y[each]>self.ymax:
  30.         self.y[each]=self.y[each]-2*self.step
  31.       for each in range(self.all):
  32.         print('鱼%d的位置是(%d,%d)'%(each,self.x[each],self.y[each]))
  33. f=Fish()
  34. f.move()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-6 10:25:44 From FishC Mobile | 显示全部楼层
会报typeError:unsupport operand type(s) for -=:'list' and 'int'我就觉得很奇怪,出去创了个列表然后让列表里的数依次加一个数没问题啊。为什么上面就有问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-6 10:28:16 | 显示全部楼层
    你的 list[each] 在哪里,是哪一句?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-6 10:28:43 From FishC Mobile | 显示全部楼层
就是这一段会错
if direction==1:
        self.y[each]+=self.step
      elif direction==2:
        self.y[each]-=self.step
      elif direction==3:
        self.x[each]-=self.step
      elif direction==4:
        self.x[each]+=self.step
求助啊求大神
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-6 10:30:52 | 显示全部楼层    本楼为最佳答案   
a1105089363 发表于 2019-8-6 10:28
就是这一段会错
if direction==1:
        self.y[each]+=self.step

  1. import random
  2. class Fish:
  3.   xmax=10
  4.   ymax=10
  5.   all=10
  6.   step=1
  7.   x=[]
  8.   y=[]
  9.   for each in range(all):#随机出生all个鱼。
  10.     x.append(random.randint(0,xmax))
  11.     y.append(random.randint(0,ymax))
  12.   def move(self):
  13.     for each in range(self.all):
  14.       direction=random.randint(1,4)#1上2下3左4右
  15.       if direction==1:
  16.         self.y[each]+=self.step
  17.       elif direction==2:
  18.         self.y[each]-=self.step
  19.       elif direction==3:
  20.         self.x[each]-=self.step
  21.       elif direction==4:
  22.         self.x[each]+=self.step
  23.       if self.x[each]<0:
  24.         self.x[each]=-self.x[each]
  25.       elif self.y[each]<0:
  26.         self.y[each]=-self.y[each]
  27.       elif self.x[each]>self.xmax:
  28.         self.x[each]=self.x[each]-2*self.step
  29.       elif self.y[each]>self.ymax:
  30.         self.y[each]=self.y[each]-2*self.step
  31.       for each in range(self.all):
  32.         print('鱼%d的位置是(%d,%d)'%(each,self.x[each],self.y[each]))
  33. f=Fish()
  34. f.move()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-6 10:32:17 From FishC Mobile | 显示全部楼层
jackz007 发表于 2019-8-6 10:28
你的 list[each] 在哪里,是哪一句?

是self.x[each]-=self.step出错,
self.step=1,self.x[each]是一个列表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-6 10:38:51 From FishC Mobile | 显示全部楼层
zltzlt 发表于 2019-8-6 10:30

你好,可以告诉我哪里错了吗,为什么你这个我复制过去就对了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-6 10:46:22 | 显示全部楼层
a1105089363 发表于 2019-8-6 10:38
你好,可以告诉我哪里错了吗,为什么你这个我复制过去就对了

int 不能和 list 相减,我把 list 改成 int 就对了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-5 02:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表