鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: RIXO

[见证历程] 暂时还不能写日志就在帖子记录一下自己学习的进度吧

[复制链接]
 楼主| 发表于 2018-9-5 20:11:44 | 显示全部楼层
第二个代码也是自己在看了30讲课后习题后写的
import os

route = input('请输入初始目录:')
name = input('请输入要查找的文件:')


def find(route,name):
    os.chdir(route+os.sep)

    files = os.listdir(os.curdir)

    for names in files :
        if name in names:
            print(os.getcwd()+os.sep+names)
        if os.path.isdir(names):
            find(names,name)
            os.chdir(os.pardir)


find(route,name)
    

这里的代码简洁了许多,但是在命名的时候我的命名单一而且容易重复,可以看出来我和答案还是有差距,而且我的输入在先,函数在后,导致逻辑上没有问题,但是看上去很乱
这个代码和上面一个代码的特点就是喜欢乱读东西,碰到拒绝访问就没有办法了,帖子问了之后找到一个办法就是忽略这个无法访问的文件
f = Flase 
try :
        os.chdir(route+os.sep)
        files = os.listdir(os.curdir)
        f = True
except:
        pass
if f:
        for names in files :
        if name in names:
            print(os.getcwd()+os.sep+names)
        if os.path.isdir(names):
            find(names,name)
            os.chdir(os.pardir)
改成这样,可靠性高多了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-5 20:13:07 | 显示全部楼层
第三个代码是利用了问到的os.walk()写的
这个函数很好用而且不会乱访问东西
import os
from os.path import join


def findfile(name,route):
    for root,dirs,files in os.walk(route+os.sep):
        for names in files:
            full_name = join(root,names)
            if name in full_name:
                print(full_name)


route = input('路径:')
name = input('文件名:')
findfile(name,route)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-11 19:24:25 | 显示全部楼层
import random as r

legal_x = [0, 10]
legal_y = [0, 10]

class Turtle:
    def __init__(self):
        # 初始体力
        self.power = 100
        # 初始位置随机
        self.x = r.randint(legal_x[0], legal_x[1])
        self.y = r.randint(legal_y[0], legal_y[1])

    def move(self):
        # 随机计算方向并移动到新的位置(x, y)
        new_x = self.x + r.choice([1, 2, -1, -2])
        new_y = self.y + r.choice([1, 2, -1, -2])
        # 检查移动后是否超出场景x轴边界
        if new_x < legal_x[0]:
            self.x = legal_x[0] - (new_x - legal_x[0])
        elif new_x > legal_x[1]:
            self.x = legal_x[1] - (new_x - legal_x[1])
        else:
            self.x = new_x
        # 检查移动后是否超出场景y轴边界
        if new_y < legal_y[0]:
            self.y = legal_y[0] - (new_y - legal_y[0])
        elif new_y > legal_y[1]:
            self.y = legal_y[1] - (new_y - legal_y[1])
        else:
            self.y = new_y        
        # 体力消耗
        self.power -= 1
        # 返回移动后的新位置
        return (self.x, self.y)

    def eat(self):
        self.power += 20
        if self.power > 100:
            self.power = 100

class Fish:
    def __init__(self):
        self.x = r.randint(legal_x[0], legal_x[1])
        self.y = r.randint(legal_y[0], legal_y[1])
        
    def move(self):
        # 随机计算方向并移动到新的位置(x, y)
        new_x = self.x + r.choice([1, -1])
        new_y = self.y + r.choice([1, -1])
        # 检查移动后是否超出场景x轴边界
        if new_x < legal_x[0]:
            self.x = legal_x[0] - (new_x - legal_x[0])
        elif new_x > legal_x[1]:
            self.x = legal_x[1] - (new_x - legal_x[1])
        else:
            self.x = new_x
        # 检查移动后是否超出场景y轴边界
        if new_y < legal_y[0]:
            self.y = legal_y[0] - (new_y - legal_y[0])
        elif new_y > legal_y[1]:
            self.y = legal_y[1] - (new_y - legal_y[1])
        else:
            self.y = new_y
        # 返回移动后的新位置
        return (self.x, self.y)

turtle = Turtle()
fish = []
for i in range(10):
    new_fish = Fish()
    fish.append(new_fish)

while True:
    if not len(fish):
        print("鱼儿都吃完了,游戏结束!")
        break
    if not turtle.power:
        print("乌龟体力耗尽,挂掉了!")
        break

    pos = turtle.move()
    # 在迭代器中删除列表元素是非常危险的,经常会出现意想不到的问题,因为迭代器是直接引用列表的数据进行引用
    # 这里我们把列表拷贝给迭代器,然后对原列表进行删除操作就不会有问题了^_^
    for each_fish in fish[:]:
        
        if each_fish.move() == pos:
            # 鱼儿被吃掉了
            turtle.eat()
            fish.remove(each_fish)
            print("有一条鱼儿被吃掉了...")
37讲最后的动动手需要注意的地方
1、移动的时候判断边界,直接写新的移动位置再判断,而不是用旧的移动位置判断
2、移动可以直接返回一个位置,不用再调用对象属性来查看,不安全,也不方便
3、在建立10条鱼的对象的时候,建立完把他扔入列表是对的,不能一直创建
4、在迭代器中删除对象需要直接拷贝列表,再删除,就是需要两个相同的列表,不能用一个列表,不然会造成错误!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 17:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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