# 将信息写入文件 with open("name.txt", "w") as file: file.write("学号: 2022001\n") file.write("姓名: 张三\n") file.write("班级: 计算机类班\n") # 读取文件并输出内容到屏幕上 with open("name.txt", "r") as file: print(file.read())
def interpolate(points, interp_point): # 对点列表按照 x 进行升序排序 points = sorted(points, key=lambda p: p[0]) # 判断要插值的点是否超出了范围 if interp_point <= points[0][0]: return points[0][1] elif interp_point >= points[-1][0]: return points[-1][1] # 找到要插值的点所在的区间 for i in range(len(points) - 1): if points[i][0] <= interp_point < points[i + 1][0]: x_left, y_left = points[i] x_right, y_right = points[i + 1] return y_left + (y_right - y_left) / (x_right - x_left) * (interp_point - x_left) points = [(1, 1), (4, 2), (3, 9), (2, 7)] interp_point = eval(input('请输入插值点:')) print(interpolate(points, interp_point))
import random def generate_code(seed): # 定义随机字符集合 charset = 'ABCDEFGHIJ0123456789' # 设定随机数种子 random.seed(seed) # 生成 6 个随机字符 code = '' for i in range(6): code += random.choice(charset) return code seed = eval(input('请输入随机数种子:')) print(generate_code(seed))
def product_nested_list(nested_lst): # 如果当前元素是数字,直接返回该数字 if isinstance(nested_lst, int): return nested_lst # 否则递归计算所有嵌套列表中的数字乘积 result = 1 for sublist in nested_lst: result *= product_nested_list(sublist) return result lst = [1, 2, [3, 4], [[[5, 6, 7]]]] print(product_nested_list(lst))
def filter_words(s): # 定义敏感词汇列表 sensitive_words = ["傻叉", "傻子", "傻冒", "二货", "呆子", "弱智"] # 遍历敏感词汇列表,将字符串中的每个敏感词汇都替换为星号 for word in sensitive_words: s = s.replace(word, "*" * len(word)) return s str = input('请输入字符串(带有不文明词汇):') print(filter_words(str))