# 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()