鱼C论坛

 找回密码
 立即注册
查看: 371|回复: 8

python类和对象

[复制链接]
发表于 2020-4-12 16:26:47 | 显示全部楼层 |阅读模式

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

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

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

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

  2. class Playground:

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

  5. class Animal_turtle(Playground):
  6.    
  7.     seat = [0,0]
  8.     life = 100
  9.    
  10.     def funtion(self):
  11.         num1 = random.randint(1,self.length)
  12.         num2 = random.randint(1,self.width)
  13.         self.seat[0] = num1
  14.         self.seat[1] = num2

  15. class Animal_fish(Playground):

  16.     seat = [0,0]
  17.    
  18.     def funtion(self):
  19.         num1 = random.randint(1,self.length)
  20.         num2 = random.randint(1,self.width)
  21.         self.seat[0] = num1
  22.         self.seat[1] = num2

  23. turtle = Animal_turtle()
  24. turtle.funtion()
  25. fish1 = Animal_fish()
  26. fish1.funtion()
  27. fish2 = Animal_fish()
  28. fish2.funtion()
  29. fish3 = Animal_fish()
  30. fish3.funtion()
  31. fish4 = Animal_fish()
  32. fish4.funtion()
  33. fish5 = Animal_fish()
  34. fish5.funtion()
  35. fish6 = Animal_fish()
  36. fish6.funtion()
  37. fish7 = Animal_fish()
  38. fish7.funtion()
  39. fish8 = Animal_fish()
  40. fish8.funtion()
  41. fish9 = Animal_fish()
  42. fish9.funtion()
  43. fish10 = Animal_fish()
  44. fish10.funtion()
  45. list_fish = [fish1,fish2,fish3,fish4,fish5,fish6,fish7,fish8,fish9,fish10]
  46. fish_list = []
  47. before = len(list_fish)

  48. while before > 0 and turtle.life > 0:
  49.    
  50.     for each in list_fish:
  51.         if each.seat == turtle.seat:
  52.             fish_list.append(list_fish.pop(each))
  53.             
  54.     while True:
  55.         #乌龟的移动判定
  56.         count1 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
  57.         count2 = random.randint(1,2)#移动的格数
  58.         if count1 == 1 and count2 == 1:
  59.             turtle.seat[0] += 1
  60.             turtle.life -= 1
  61.         elif count1 == 1 and count2 == 2:
  62.             turtle.seat[0] += 2
  63.             turtle.life -= 1
  64.         elif count1 == 2 and count2 == 1:
  65.             turtle.seat[0] -= 1
  66.             turtle.life -= 1
  67.         elif count1 == 2 and count2 == 2:
  68.             turtle.seat[0] -= 2
  69.             turtle.life -= 1
  70.         elif count1 == 3 and count2 == 1:
  71.             turtle.seat[1] -= 1
  72.             turtle.life -= 1
  73.         elif count1 == 3 and count2 == 2:
  74.             turtle.seat[1] -= 2
  75.             turtle.life -= 1
  76.         elif count1 == 4 and count2 == 1:
  77.             turtle.seat[1] += 1
  78.             turtle.life -= 1
  79.         elif count1 == 4 and count2 == 2:
  80.             turtle.seat[1] += 2
  81.             turtle.life -= 1
  82.         elif turtle.seat[0] >= Playground().length:
  83.             turtle.seat[0] -= 1
  84.             turtle.life -= 1
  85.         elif turtle.seat[0] <= 0:
  86.             turtle.seat[0] += 1
  87.             turtle.life -= 1
  88.         elif turtle.seat[1] >= Playground().width:
  89.             turtle.seat[1] -= 1
  90.             turtle.life -= 1
  91.         elif turtle.seat[1] <= 0:
  92.             turtle.seat[1] += 1
  93.             turtle.life -= 1
  94.         break
  95.     while True:
  96.         #鱼的移动判定
  97.         for each in list_fish:
  98.             count3 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
  99.             if count3 == 1:
  100.                 each.seat[0] += 1
  101.             elif count3 == 2:
  102.                 each.seat[0] -= 1
  103.             elif count3 == 3:
  104.                 each.seat[1] -= 1
  105.             elif count3 == 4:
  106.                 each.seat[1] += 1
  107.             elif each.seat[0] >= Playground().length:
  108.                 each.seat[0] -= 1
  109.             elif each.seat[0] <= 0:
  110.                 each.seat[0] += 1
  111.             elif each.seat[1] >= Playground().width:
  112.                 each.seat[1] -= 1
  113.             elif each.seat[1] <= 0:
  114.                 each.seat[1] += 1
  115.         break

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

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

NZ8P{]F(AZYGB}50NNDT3{G.png
BO7XKI7HSB5]2LEAJTI)@7M.png
EP~5EJ$5CWJHE}V}0_%OL]4.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-12 16:27:38 | 显示全部楼层
有时报错有时不报错的代码这我还是第一次遇到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-12 16:31:47 | 显示全部楼层
列表的 pop 函数是传的是整形数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 16:41:08 | 显示全部楼层
lixiangyv 发表于 2020-4-12 16:31
列表的 pop 函数是传的是整形数据

多谢,不过只是小问题,代码依旧报错。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 16:58:27 | 显示全部楼层
难道我代码太长了,所以没人想看?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-12 17:32:04 From FishC Mobile | 显示全部楼层
二楼说的问题你确定解决了吗?代码59行的pop函数使用时应该空参或者传入一个整数索引,但你传入了一个对象。你目前代码报的错误就是这个。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 17:41:22 | 显示全部楼层
  1. while before > 0 and turtle.life > 0:
  2.    
  3.     for each in list_fish:
  4.         if each.seat == turtle.seat:
  5.             index_value = list_fish.index(each)
  6.             list_fish.remove(each)
  7.             fish_list2.append(fish_list1[index_value])
复制代码

这是那段代码修改后的样子,我用成remove
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

不过倒是不报错了,只是还是有些问题。
@F37${0H]]6(GLU}1W_Q9]5.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 17:44:35 | 显示全部楼层
hhjdsd 发表于 2020-4-12 17:43
不过倒是不报错了,只是还是有些问题。

楼上的代码:
  1. import random

  2. class Playground:

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

  5. class Animal_turtle(Playground):
  6.    
  7.     seat = [0,0]
  8.     life = 100
  9.    
  10.     def funtion(self):
  11.         num1 = random.randint(1,self.length)
  12.         num2 = random.randint(1,self.width)
  13.         self.seat[0] = num1
  14.         self.seat[1] = num2

  15. class Animal_fish(Playground):

  16.     seat = [0,0]
  17.    
  18.     def funtion(self):
  19.         num1 = random.randint(1,self.length)
  20.         num2 = random.randint(1,self.width)
  21.         self.seat[0] = num1
  22.         self.seat[1] = num2

  23. turtle = Animal_turtle()
  24. turtle.funtion()
  25. fish1 = Animal_fish()
  26. fish1.funtion()
  27. fish2 = Animal_fish()
  28. fish2.funtion()
  29. fish3 = Animal_fish()
  30. fish3.funtion()
  31. fish4 = Animal_fish()
  32. fish4.funtion()
  33. fish5 = Animal_fish()
  34. fish5.funtion()
  35. fish6 = Animal_fish()
  36. fish6.funtion()
  37. fish7 = Animal_fish()
  38. fish7.funtion()
  39. fish8 = Animal_fish()
  40. fish8.funtion()
  41. fish9 = Animal_fish()
  42. fish9.funtion()
  43. fish10 = Animal_fish()
  44. fish10.funtion()
  45. list_fish = [fish1,fish2,fish3,fish4,fish5,fish6,fish7,fish8,fish9,fish10]
  46. fish_list1 = ['fish1','fish2','fish3','fish4','fish5'\
  47.              ,'fish6','fish7','fish8','fish9','fish10']
  48. fish_list2 = []
  49. before = len(list_fish)

  50. while before > 0 and turtle.life > 0:
  51.    
  52.     for each in list_fish:
  53.         if each.seat == turtle.seat:
  54.             index_value = list_fish.index(each)
  55.             list_fish.remove(each)
  56.             fish_list2.append(fish_list1[index_value])
  57.             
  58.     while True:
  59.         #乌龟的移动判定
  60.         count1 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
  61.         count2 = random.randint(1,2)#移动的格数
  62.         if count1 == 1 and count2 == 1:
  63.             turtle.seat[0] += 1
  64.             turtle.life -= 1
  65.         elif count1 == 1 and count2 == 2:
  66.             turtle.seat[0] += 2
  67.             turtle.life -= 1
  68.         elif count1 == 2 and count2 == 1:
  69.             turtle.seat[0] -= 1
  70.             turtle.life -= 1
  71.         elif count1 == 2 and count2 == 2:
  72.             turtle.seat[0] -= 2
  73.             turtle.life -= 1
  74.         elif count1 == 3 and count2 == 1:
  75.             turtle.seat[1] -= 1
  76.             turtle.life -= 1
  77.         elif count1 == 3 and count2 == 2:
  78.             turtle.seat[1] -= 2
  79.             turtle.life -= 1
  80.         elif count1 == 4 and count2 == 1:
  81.             turtle.seat[1] += 1
  82.             turtle.life -= 1
  83.         elif count1 == 4 and count2 == 2:
  84.             turtle.seat[1] += 2
  85.             turtle.life -= 1
  86.         elif turtle.seat[0] >= Playground().length:
  87.             turtle.seat[0] -= 1
  88.             turtle.life -= 1
  89.         elif turtle.seat[0] <= 0:
  90.             turtle.seat[0] += 1
  91.             turtle.life -= 1
  92.         elif turtle.seat[1] >= Playground().width:
  93.             turtle.seat[1] -= 1
  94.             turtle.life -= 1
  95.         elif turtle.seat[1] <= 0:
  96.             turtle.seat[1] += 1
  97.             turtle.life -= 1
  98.         break
  99.     while True:
  100.         #鱼的移动判定
  101.         for each in list_fish:
  102.             count3 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
  103.             if count3 == 1:
  104.                 each.seat[0] += 1
  105.             elif count3 == 2:
  106.                 each.seat[0] -= 1
  107.             elif count3 == 3:
  108.                 each.seat[1] -= 1
  109.             elif count3 == 4:
  110.                 each.seat[1] += 1
  111.             elif each.seat[0] >= Playground().length:
  112.                 each.seat[0] -= 1
  113.             elif each.seat[0] <= 0:
  114.                 each.seat[0] += 1
  115.             elif each.seat[1] >= Playground().width:
  116.                 each.seat[1] -= 1
  117.             elif each.seat[1] <= 0:
  118.                 each.seat[1] += 1
  119.         break

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

  123. if turtle.life <= 0 and before > 0:
  124.     print('乌龟失败啦,他最终还是没有胜利。\n不幸牺牲的小鱼有:',end='')
  125.     for each in fish_list2:
  126.         print(each,end=' ')
  127. if before == 0 and turtle.life >0:
  128.     print('乌龟胜利啦!!\n他还剩下%d点体力值。' % turtle.life)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 16:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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