1、持续输入数据、输完之后退出
with open('test4.txt','w',encoding='utf-8') as f: while True: inp = input("请输入内容:") if inp == 'q': break f.write(inp + '\n') with open('test4.txt','r',encoding='utf-8') as f: print(f.read())
2、验证用户名和密码,用户名和密码存在文件中
with open('test3.py',encoding='utf8') as f: d = dict() for line in f: vvv=line.strip('\n').split(":")[0] kkk=line.strip('\n').split(":")[1] if not len(line): continue d[vvv] = kkk while True: a = input("用户名:").strip() b = input("密码:").strip() if a in d.keys() and b == d[a]: print("登陆成功") else: print("用户名或密码错误")
3、生成5位的随机验证码
import random sts = '' for i in range(5): sts += random.choice([str(random.randint(0,9)),chr(random.choice([random.randint(65,90),random.randint(97,122)]))]) print(sts)
4、题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
lis = [1,2,3,4]
k=[]
for a in lis:
for b in lis:
for c in lis:
d=str(a)+str(b)+str(c)
if d[0] != d[1] and d[1] != d[2] and d[0] !=d[2]:
k.append(int(d))
print(len(k))
print(k)
24
[123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]