Pythpn作业第六天

# 1.
# 使用递归去列出当前目录下的所有文件(格式要求:分层输出)
# 要求:最少4层目录: 比如:
# test:
# -test.txt
# -test_data.xls
# -test1
# -test1.txt
# -test_data1.txt
# -test2
# -test2.txt
# -test2_data.txt
# -test3
# -test3.txt
# -test3_data.txt

import os

path = "E:\\test"


def list_dir(path):
    # 判断路径是否存在
    if os.path.exists(path):
        # 列出目录中所有文件和目录
        file_list = os.listdir(path)
        for file in file_list:
            if os.path.isfile(os.path.join(path, file)):
                print(file)
        for file in file_list:
            if os.path.isdir(os.path.join(path, file)):
                print(file)
                list_dir(os.path.join(path, file))


print(path)
list_dir(path)

print(100 * "-")
# 2.
# 给定一个成绩score,随机出8个分数 =》 8个分数之和/8 = 80,
# 8个分数的分布,score - 10 < score < score + 10
# 提示使用random中choices和sample
from random import choices, sample

score = 80
while True:
    list_data1 = sample(range(score - 10, score + 1), 4)
    list_data2 = sample(range(score, score + 11), 4)
    sum_data = sum(list_data1) + sum(list_data2)
    if sum_data / 8 == score:
        print(list_data1, list_data2)
        break

print(100 * "-")


# 3.
# 定义一个类:Person
# 类属性:type = "student"
# 对象属性:name, age, gender
# 方法:print_info: 打印内容:某某 is a good student.
# 在类中重写:__new__和__init__, 并打印__new__和__init__来显示已调用
# 实例化两个对象: zhangsan, lisi且调用方法:
class Person(object):
    type = "student"

    def print_info(self):
        print(self.name, "is a good student.")

    def __new__(cls, *args, **kwargs):
        print("__new__")
        return super(Person, cls).__new__(cls)

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

        print("__init__")


zhangsan = Person("张三", 23, "男")
zhangsan.print_info()
lisi = Person("李四", 22, "男")
lisi.print_info()

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值