# 随便输入一段字符串,计算整数的个数 info = input('>>>').strip() for i in info: if i.isalpha(): info = info.replace(i, " ") l = info.split() print(len(l)) # ab互换 a = 1 b = 2 a,b = b,a print(a,b) # 循环迭代字符串(不需要知道字符串多长) s = "qwertyuiopasdfghjkl" index = 0 while 1: print(s[index]) index += 1 if index == len(s): break # 有如下值li= [11,22,33,44,55,66,77,88,99],将所有小于 66 的值 保存至字典的第一个key中,将大于 66 的值保存至第二个key的值中。 即: {'k1': 小于66的所有值列表, 'k2': 大于66的所有值列表} li = [11, 22, 33, 44, 55, 66, 77, 88, 99] dic = {} l_greater = [] #大于66的所有值列表 l_less = [] #小于66的所有值列表 for i in li: if i < 66: l_less.append(i) elif i > 66: l_greater.append(i) dic.setdefault("k1", l_less) dic.setdefault("k2", l_greater) print(dic) # 输出商品列表,用户输入序号,显示用户选中的商品 商品 li = ["手机", "电脑", '鼠标垫', '游艇'] 要求: 1:页面显示 序号 + 商品名称,如: 1 手机 2 电脑 ... 2:用户输入选择的商品序号,然后打印商品名称 3:如果用户输入的商品序号有误,则提示输入有误,并重新输入 4:用户输入Q或者q,退出程序 flag = True while flag: li = ["手机", "电脑", "鼠标垫", "游艇"] for i in li: print("{} {}".format(li.index(i)+1, i)) num_of_chioce = input("请输入选择的商品序号/输入Q或者q退出程序:") if num_of_chioce.isdigit(): num_of_chioce = int(num_of_chioce) if num_of_chioce > 0 and num_of_chioce <= len(li): print(li[num_of_chioce-1]) else:print("请输入有效数字") elif num_of_chioce.upper() == "Q": break else: print("请输入数字") # 让用户选择何时退出 x = "" while x != "quit": x = input("如果想退出,请输入quit(不区分大小写:").lower() if x != "quit": #退出成功时不再打印输入内容 print(x) active = True while active: g_input = input("如果想退出,请输入quit(不区分大小写):").lower() if g_input == "quit": active = False else: print(g_input) # 使用用户输入来填充字典 dic = {} #定义一个空字典 active = True #设置一个标志 while active: name = input("您叫什么名字?") response = input("请问您喜欢什么户外运动?") dic[name] = response #名字和答案以键值对存储进字典 repeat = input("非常感谢您的配合!任意键退出/修改请输入yes:") repeat = repeat.lower() #大小写不区分 if repeat != "yes": active = False for name, response in dic.items(): #遍历字典 print(" 姓名:" + name + " 爱好:" + response) #换行打印 # 注册、保存账号密码、3次机会验证登录 while 1: name = input("请输入注册账号:") password = input("请输入注册密码:") confirm_password = input("请再一次确认密码:") if confirm_password == password: print("恭喜您注册成功") with open("text", mode="w+", encoding="utf-8") as file: file.write("{} {}".format(name, password)) break else: continue i = 0 lis = [] while i < 3: use = input("请输入登录账号:") psd = input("请输入登录密码:") with open("text", mode="r+", encoding="utf-8") as file: for x in file: lis.append(x) if use == name and psd == password: print("登录成功") break else: print("登录失败") i += 1 # 用户输入内容,计算"索引为奇数且对应的元素为数字"的个数 content = input(">>>:") count = 0 for i in range(len(content)): if i % 2 != 0 and content[i].isdigit(): count += 1 print(count) # 有列表lis = [1, 2, 3, 4, 5, 7, 8, 9],将所有小于6的值保存至字典 的key1中,将大于6的值保存至字典的key2中 lis = [1, 2, 3, 4, 5, 7, 8, 9] result = {"key1": [], "key2": []} for i in lis: if i < 6: if "key1" not in result: result["key1"] = [] result["key1"].append(i) else: if "key2" not in result: result["key2"] = [] result["key2"].append(i) print(result) lis = [1, 2, 3, 4, 5, 7, 8, 9] result = {} result.setdefault("key1", []) result.setdefault("key2", []) for i in lis: if i < 6: result["key1"].append(i) else: result["key2"].append(i) print(result) # 移除列表lis每个元素的空格、找出以"A"或者"a"开头、并以"c"结尾的所有元素,将这些元素添加到新列表中 lis = ["taibai", "alexC", "AbC", "egon", "Ritian", "Wusir", " aqc"] lis1 = [] lis2 = [] lis3 = [] for i in lis: lis1.append(i.strip()) print(lis1) for i1 in lis1: if i1.startswith("A") or i1.startswith("a"): lis2.append(i1) for i2 in lis2: if i2.endswith("c"): lis3.append(i2) print(lis3) # 写程序:模拟公司HR录入员工账号密码的程序 1)员工的账号密码存储在这种数据类型中 user_list = [{"username": "barry", "password": "1234"} {"username": "ppd", "password": "123456"} ..................................... ] 2)非法字符模板:board = ["张三", "李小四", "王二麻子"] 3)HR输入用户名、密码(可持续输入,如果想终止程序,那就在输入用户名时输入Q或者q),在 HR输入用户名时,检测此用户名是否有board里面的非法字符,如果有非法字符,则将非法字符替 换成同数量的*,然后添加到user_list中,如果没有非法字符,则直接添加到user_list中,每次 添加成功后,打印出刚添加的用户名、密码。 user_list = [] board = ["张三", "李小四", "王二麻子"] while 1: u_name = input("请输入用户名、退出程序请输入Q或者q:") if u_name.upper() == "Q": break for i in range(len(board)): if u_name == board[i]: u_name = len(board[i])*"*" ps_wd = input("请输入用户密码:") user_list.append({"username": u_name, "password": ps_wd}) print(user_list) # 写函数,判断用户传入的值(字符串、列表、元组)的长度是否大于5 def s_lenth(s): if len(s) > 5: return "right" ret = s_lenth("123456") print(ret) # 写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容,并返回结果 def func(x): if type(x) is str: for i in x: if i == ' ': return True elif x and type(x) is list or type(x) is tuple: for i in x: if not i: return True elif not x: return True print(func()) # 写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作 import os def alter_content(file, old_str, new_str): """ 将替换的字符串写到一个新的文件中,然后将原文件删除,新文件改为原来文件的名字 file: 文件路径 old_str: 需要替换的字符串 new_str: 替换的字符串 return: None """ with open(testfile, "r", encoding="utf-8") as f1, open("%s.bak" % testfile, "w", encoding="utf-8") as f2: for line in f1: if old_str in line: line = line.replace(old_str, new_str) f2.write(line) os.remove(testfile) os.rename("%s.bak" % testfile, testfile) alter_content("test", "你好", "测试") # 两个数相乘,计算结果保留2位小数 a = eval(input()) b = eval(input()) print("面积是:%.5f" % (a*b)) # 斐波那锲数列1,1,2,3,5,8,13...根据这样的规律,求出400 万以内最大的斐波那锲数,并求出是第几个斐波那锲数 def count(a, b): n = 0 while b < 4000000: a, b = b, a+b n = n + 1 return n, a count(0, 1) # 求第n个斐波那锲数 def recursion(n): if n <= 2: return 1 return recursion(n-1)+recursion(n-2) recursion(5) # 处理文件,用户指定要查找的文件和内容,将文件中包含要查找内容的每一行都输出到屏幕 def check_file(filename,check_content): with open(filename, encoding="utf-8") as f: for i in f: if check_content in i: yield i generator = check_file("text", "id") for i in generator: print(i) # 如下,每个小字典的name对应股票名字,shares对应多少股,price对应股票的价格。 计算购买每支股票的总价 portfolio = [{'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65}] res = map(lambda dic:{dic["name"]:round(dic["shares"]*dic["price"], 2)},portfolio) # 用递归实现阶乘 def factorial(n): print(n) if n == 0: #等于0就运算完了 return 1 return n * factorial(n - 1) #每次递归相乘,n值都比之前小1 res = factorial(4) print(res) # 随意写一个n行以上的文件 1.运行程序,先将内容读到内存中,用列表存储 2.接收用户输入页码,每页5条,仅输出当页的内容 def check_content(filename): with open(filename, "r", encoding="utf-8") as f: lis = f.readlines() pages, row = divmod(len(lis), 5) if row: pages += 1 while True: page_num = input("请输入页码或者输入Q/q退出:") if page_num.isdigit(): page_num = int(page_num) if page_num <= 0 or page_num > pages: print("