零基础python 37课后练习中的一些问题
在面相对象编程 小甲鱼 零基础python37课 的练习中,有些问题不是很明白, 我把它们标注在了代码中, 求大佬们拍打小萌新。 爱你们{:10_298:}import random as r
legal_x =
legal_y =
class Turtle:
def __init__(self):
self.power = 100
# the initial location is random
self.x = r.randint(legal_x, legal_x)
# the num in [] is index, this line means generating random int between 0 and 10
self.y = r.randint(legal_y, legal_y)
def move(self):
# move to a new location (x,y) without a firm direction
new_x = self.x + r.choice()
new_y = self.y + r.choice()
# check if the new location is outside of the required area (x)
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x= legal_x - (new_x - legal_x)
else:
self.x = new_x
# check if the new location is outside of the required area (x)
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
# power customise
self.power -=1
# return the new location
return (self.x, self.y)
def eat(self):
self.power += 20
ifself.power > 100:
self.power = 100
class Fish:
def __init__(self):
self.x = r.randint(legal_x, legal_x)
self.y = r.randint(legal_y, legal_y)
def move(self):
# move to a new location (x,y) without a firm direction
new_x = self.x + r.choice()
new_y = self.y + r.choice()
# check if the new location is outside of the required area (x)
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
# check if the new location is outside of the required area (x)
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
# return the new location
return (self.x, self.y)
turtle = Turtle()
fish = []
for i in range(10):
new_fish=Fish()
fish.append(new_fish)
while True:# 问题一: while True, 这里判断的是什么条件为True啊?
if not len(fish): # 问题二: 为什么这里判断fish列表为空呢?? if not len(fish) 在这里的用法??
print('all fishes have been eaten, game over')
break
if not turtle.power: # 问题三: 为什么这里不是 if turtle.power==0 这个条件呢?
print('the turtle has died because of the lack of the power')
break
pos = turtle.move()
for each_fish in fish[:]: # 问题四: 为什么这里不直接 for each_fish in fish ?
if each_fish.move() == pos:
# same location, fish is eaten
turtle.eat()
fish.remove(each_fish)
print('there is a fish was eaten, OMG')
本帖最后由 Twilight6 于 2020-5-23 20:33 编辑
# 问题一: while True, 这里判断的是什么条件为True啊?因为要让程序一直循环 ,乌龟吃鱼又是随机的,直到 if not len(fish) 或 if not turtle.power 条件成立了 执行里面退出循环的语句# 问题二: 为什么这里判断fish列表为空呢?? if not len(fish) 在这里的用法??因为每次被乌龟吃了,这条鱼就要从列表中移除出去,直到乌龟把鱼全部吃了,列表为空,判定乌龟胜利,退出循环,if not len(fish) 如果fish为空列表 则长度为 0 可以视为布尔类型值 False 加上 not 后 条件从False 变成了 True # 问题三: 为什么这里不是 if turtle.power==0 这个条件呢?条件可以为if turtle.power==0 ,前面看错题目了 订正这后面就不删了: power 这里是代表乌龟的生命值,在刚开始时候生命值一定大于 0所以如果你改成 if turtle.power==0 这个条件 一开始就会成立,导致直接判定乌龟失败,而你加上 not 之后 (not 0)是等于 True的,只要乌龟生命不为 0就不会判断乌龟失败了 # 问题四: 为什么这里不直接 for each_fish in fish ?因为小甲鱼说过 直接 for each_fish in fish 会出现一些不可预料的事情 哈哈 Twilight6 发表于 2020-5-23 20:18
因为要让程序一直循环 ,乌龟吃鱼又是随机的,直到 if not len(fish) 或 if not turtle.power 条件成立了...
第三点有个疑惑if not turtle.power 和 if turtle.power == 0 不是一个意思咩?if turtle.power==0 这个条件 一开始不会成立吧{:10_250:} Twilight6 发表于 2020-5-23 12:18
因为要让程序一直循环 ,乌龟吃鱼又是随机的,直到 if not len(fish) 或 if not turtle.power 条件成立了...
问题2 和 问题3还是很迷。。。。 特别是问题三,在定义小乌龟这个类的时候, 不是就已经初始化power为100了么, 所以应该不会出现小乌龟一出来就死掉的情况呀???? hwang.me 发表于 2020-5-23 21:19
问题2 和 问题3还是很迷。。。。 特别是问题三,在定义小乌龟这个类的时候, 不是就已经初始化power为1 ...
问题三订正后面是我写错了 只是不删了 意思 hwang.me 发表于 2020-5-23 21:19
问题2 和 问题3还是很迷。。。。 特别是问题三,在定义小乌龟这个类的时候, 不是就已经初始化power为1 ...
问题二 fish 列表是存鱼的条数的,你每次被小乌龟吃掉一只就移除一只 直到列表全部被吃完,小乌龟胜利,
当列表全部被吃完时候len(fish) == 0 的0 等价于 False 你加上not 就变成了 Trueif 条件就成立 开始执行if 里面的语句
问题三:条件可以为if turtle.power==0 本帖最后由 hwang.me 于 2020-5-23 19:01 编辑
Twilight6 发表于 2020-5-23 13:31
问题二 fish 列表是存鱼的条数的,你每次被小乌龟吃掉一只就移除一只 直到列表全部被吃完,小乌龟胜利,
...
谢谢你 我好像能理解了, 图片里的这两个代码块 是不是其实是可以互换位置的?
hwang.me 发表于 2020-5-24 02:59
谢谢你 我好像能理解了, 图片里的这两个代码块 是不是其实是可以互换位置的?
可以 但是while True 位置不能动 {:5_110:}
页:
[1]