首页 > 其他 > 详细

练习题

时间:2020-04-11 10:24:11      阅读:44      评论:0      收藏:0      [点我收藏+]

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]

 

练习题

原文:https://www.cnblogs.com/sxdpython/p/12670740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!