hhjdsd 发表于 2020-4-12 16:26:47

python类和对象

本帖最后由 hhjdsd 于 2020-4-12 19:15 编辑

代码可能有些长,如有其他错误的地方,请帮忙指出。
import random

class Playground:

    length = random.randint(1,10)
    width = random.randint(1,10)

class Animal_turtle(Playground):
   
    seat =
    life = 100
   
    def funtion(self):
      num1 = random.randint(1,self.length)
      num2 = random.randint(1,self.width)
      self.seat = num1
      self.seat = num2

class Animal_fish(Playground):

    seat =
   
    def funtion(self):
      num1 = random.randint(1,self.length)
      num2 = random.randint(1,self.width)
      self.seat = num1
      self.seat = num2

turtle = Animal_turtle()
turtle.funtion()
fish1 = Animal_fish()
fish1.funtion()
fish2 = Animal_fish()
fish2.funtion()
fish3 = Animal_fish()
fish3.funtion()
fish4 = Animal_fish()
fish4.funtion()
fish5 = Animal_fish()
fish5.funtion()
fish6 = Animal_fish()
fish6.funtion()
fish7 = Animal_fish()
fish7.funtion()
fish8 = Animal_fish()
fish8.funtion()
fish9 = Animal_fish()
fish9.funtion()
fish10 = Animal_fish()
fish10.funtion()
list_fish =
fish_list = []
before = len(list_fish)

while before > 0 and turtle.life > 0:
   
    for each in list_fish:
      if each.seat == turtle.seat:
            fish_list.append(list_fish.pop(each))
            
    while True:
      #乌龟的移动判定
      count1 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
      count2 = random.randint(1,2)#移动的格数
      if count1 == 1 and count2 == 1:
            turtle.seat += 1
            turtle.life -= 1
      elif count1 == 1 and count2 == 2:
            turtle.seat += 2
            turtle.life -= 1
      elif count1 == 2 and count2 == 1:
            turtle.seat -= 1
            turtle.life -= 1
      elif count1 == 2 and count2 == 2:
            turtle.seat -= 2
            turtle.life -= 1
      elif count1 == 3 and count2 == 1:
            turtle.seat -= 1
            turtle.life -= 1
      elif count1 == 3 and count2 == 2:
            turtle.seat -= 2
            turtle.life -= 1
      elif count1 == 4 and count2 == 1:
            turtle.seat += 1
            turtle.life -= 1
      elif count1 == 4 and count2 == 2:
            turtle.seat += 2
            turtle.life -= 1
      elif turtle.seat >= Playground().length:
            turtle.seat -= 1
            turtle.life -= 1
      elif turtle.seat <= 0:
            turtle.seat += 1
            turtle.life -= 1
      elif turtle.seat >= Playground().width:
            turtle.seat -= 1
            turtle.life -= 1
      elif turtle.seat <= 0:
            turtle.seat += 1
            turtle.life -= 1
      break
    while True:
      #鱼的移动判定
      for each in list_fish:
            count3 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
            if count3 == 1:
                each.seat += 1
            elif count3 == 2:
                each.seat -= 1
            elif count3 == 3:
                each.seat -= 1
            elif count3 == 4:
                each.seat += 1
            elif each.seat >= Playground().length:
                each.seat -= 1
            elif each.seat <= 0:
                each.seat += 1
            elif each.seat >= Playground().width:
                each.seat -= 1
            elif each.seat <= 0:
                each.seat += 1
      break

    after = len(list_fish)
    turtle.life += (before-after)*20
    before = after

if turtle.life <= 0 and before > 0:
    print('乌龟失败啦,他最终还是没有胜利。\n不幸牺牲的小鱼有:',end='')
    for each in fish_list:
      print(each,end=' ')
if before == 0 and turtle.life >0:
    print('乌龟胜利啦!!\n他还剩下%d点体力值。' % turtle.life)

hhjdsd 发表于 2020-4-12 16:27:38

有时报错有时不报错的代码这我还是第一次遇到{:10_266:}

lixiangyv 发表于 2020-4-12 16:31:47

列表的 pop 函数是传的是整形数据

hhjdsd 发表于 2020-4-12 16:41:08

lixiangyv 发表于 2020-4-12 16:31
列表的 pop 函数是传的是整形数据

多谢,不过只是小问题,代码依旧报错。

hhjdsd 发表于 2020-4-12 16:58:27

{:10_272:}难道我代码太长了,所以没人想看?

倒戈卸甲 发表于 2020-4-12 17:32:04

二楼说的问题你确定解决了吗?代码59行的pop函数使用时应该空参或者传入一个整数索引,但你传入了一个对象。你目前代码报的错误就是这个。

hhjdsd 发表于 2020-4-12 17:41:22

while before > 0 and turtle.life > 0:
   
    for each in list_fish:
      if each.seat == turtle.seat:
            index_value = list_fish.index(each)
            list_fish.remove(each)
            fish_list2.append(fish_list1)
这是那段代码修改后的样子,我用成remove

hhjdsd 发表于 2020-4-12 17:43:31

倒戈卸甲 发表于 2020-4-12 17:32
二楼说的问题你确定解决了吗?代码59行的pop函数使用时应该空参或者传入一个整数索引,但你传入了一个对象 ...

不过倒是不报错了,只是还是有些问题。

hhjdsd 发表于 2020-4-12 17:44:35

hhjdsd 发表于 2020-4-12 17:43
不过倒是不报错了,只是还是有些问题。

楼上的代码:
import random

class Playground:

    length = random.randint(1,10)
    width = random.randint(1,10)

class Animal_turtle(Playground):
   
    seat =
    life = 100
   
    def funtion(self):
      num1 = random.randint(1,self.length)
      num2 = random.randint(1,self.width)
      self.seat = num1
      self.seat = num2

class Animal_fish(Playground):

    seat =
   
    def funtion(self):
      num1 = random.randint(1,self.length)
      num2 = random.randint(1,self.width)
      self.seat = num1
      self.seat = num2

turtle = Animal_turtle()
turtle.funtion()
fish1 = Animal_fish()
fish1.funtion()
fish2 = Animal_fish()
fish2.funtion()
fish3 = Animal_fish()
fish3.funtion()
fish4 = Animal_fish()
fish4.funtion()
fish5 = Animal_fish()
fish5.funtion()
fish6 = Animal_fish()
fish6.funtion()
fish7 = Animal_fish()
fish7.funtion()
fish8 = Animal_fish()
fish8.funtion()
fish9 = Animal_fish()
fish9.funtion()
fish10 = Animal_fish()
fish10.funtion()
list_fish =
fish_list1 = ['fish1','fish2','fish3','fish4','fish5'\
             ,'fish6','fish7','fish8','fish9','fish10']
fish_list2 = []
before = len(list_fish)

while before > 0 and turtle.life > 0:
   
    for each in list_fish:
      if each.seat == turtle.seat:
            index_value = list_fish.index(each)
            list_fish.remove(each)
            fish_list2.append(fish_list1)
            
    while True:
      #乌龟的移动判定
      count1 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
      count2 = random.randint(1,2)#移动的格数
      if count1 == 1 and count2 == 1:
            turtle.seat += 1
            turtle.life -= 1
      elif count1 == 1 and count2 == 2:
            turtle.seat += 2
            turtle.life -= 1
      elif count1 == 2 and count2 == 1:
            turtle.seat -= 1
            turtle.life -= 1
      elif count1 == 2 and count2 == 2:
            turtle.seat -= 2
            turtle.life -= 1
      elif count1 == 3 and count2 == 1:
            turtle.seat -= 1
            turtle.life -= 1
      elif count1 == 3 and count2 == 2:
            turtle.seat -= 2
            turtle.life -= 1
      elif count1 == 4 and count2 == 1:
            turtle.seat += 1
            turtle.life -= 1
      elif count1 == 4 and count2 == 2:
            turtle.seat += 2
            turtle.life -= 1
      elif turtle.seat >= Playground().length:
            turtle.seat -= 1
            turtle.life -= 1
      elif turtle.seat <= 0:
            turtle.seat += 1
            turtle.life -= 1
      elif turtle.seat >= Playground().width:
            turtle.seat -= 1
            turtle.life -= 1
      elif turtle.seat <= 0:
            turtle.seat += 1
            turtle.life -= 1
      break
    while True:
      #鱼的移动判定
      for each in list_fish:
            count3 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
            if count3 == 1:
                each.seat += 1
            elif count3 == 2:
                each.seat -= 1
            elif count3 == 3:
                each.seat -= 1
            elif count3 == 4:
                each.seat += 1
            elif each.seat >= Playground().length:
                each.seat -= 1
            elif each.seat <= 0:
                each.seat += 1
            elif each.seat >= Playground().width:
                each.seat -= 1
            elif each.seat <= 0:
                each.seat += 1
      break

    after = len(list_fish)
    turtle.life += (before-after)*20
    before = after

if turtle.life <= 0 and before > 0:
    print('乌龟失败啦,他最终还是没有胜利。\n不幸牺牲的小鱼有:',end='')
    for each in fish_list2:
      print(each,end=' ')
if before == 0 and turtle.life >0:
    print('乌龟胜利啦!!\n他还剩下%d点体力值。' % turtle.life)
页: [1]
查看完整版本: python类和对象