python总结与习题(一)

python总结与习题(一)

python总结

相关习题

1.实现三次认证

实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!
实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

username = "seven"
password = "123"
count = 1
while  count <= 3:
    name = input("name>>:").strip()
    psword = input("password>>:").strip()
    if username == name and psword == password:
        print("登录成功")
        break
    else:
        print("登录失败")
        count += 1

2.判断闰年

输入一年份,判断该年份是否是闰年并输出结果。
注:凡符合下面两个条件之一的年份是闰年。 (1) 能被4整除但不能被100整除。 (2) 能被400整除。

years = int(input("year:"))
if years % 4 == 0 and years % 100 != 0 or years % 400 == 0:
    print(years,"是闰年")
else:
    print(years,"不是闰年")

注解:关于闰年
1 如果年份能被4整除,则该年计为闰年;
2 如果年份能被100整除,则不计为闰年;
3 如果年份能被400整除,则计为闰年。
100,200,300不是 400是

3.计算利率

假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?

interest_rate = 0.0325
capital = 10000
total_money = capital*(1+interest_rate)
total_years = 1
while total_money <= 20000:
    print("years:%d money:%d"%(total_years,total_money))
    total_money *= 1+interest_rate
    total_years += 1

4.while输出倒三角

使用while,完成以下图形的输出

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
count = 0
while count < 9:
    if count < 5:
        count += 1
        print("*"*count)
    else:
        count += 1
        print("*"*(9-count))

5.计算业绩

根据销售额给员工发提成,提成为阶梯制,假设一个销售人员基本工资为3000元, 每月业绩低于5万元,无提成,5万至10万,提成3%,10万至15万提成5%,15万-25万提成8%,25万至35万提成10%,35万以上,提成15%。 从键盘获取用户当月业绩,计算其工资+提成的总额。

fundamental_wages = 3000
achievement = int(input("outstanding:").strip())  #业绩
salary = 0
if achievement < 50000:
    salary = fundamental_wages
elif achievement <= 100000:
    salary = (1+0.03)*fundamental_wages
elif achievement <= 150000:
    salary = (1+0.05)*fundamental_wages
elif achievement <= 250000:
    salary = (1+0.08)*fundamental_wages
elif achievement <= 350000:
    salary = (1+0.1)*fundamental_wages
else:
    salary = (1+0.15)*fundamental_wages
print("salary:%d"%salary)

注解:
python没有switch
可以通过函数字典映射和类的调度方法实现
例如:

switch = {
    "a":lambda x: x*3,
    "b":lambda x: x*2,
    "c":lambda x: x**2,
}

6.计算地铁区段价格

北京地铁交通价格调整为:
6公里(含)内3元;
6公里至12公里(含)4元;
12公里至22公里(含)5元;
22公里至32公里(含)6元;
32公里以上部分,
每增加1元可乘坐20公里。使用市政交通一卡通刷卡乘坐轨道交通,每自然月内每张卡支出累计满100元以后的乘次价格给予8折优惠;满150元以后的乘次给予5折优惠,
假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的地铁,编写程序,从键盘获取距离,帮小明计算每月的总花费。

total_expense = 0
times = 1
while times <= 20:
    distance = int(input("distance>>:").strip())
    if distance <= 6:
        per_times_expense = 3
    elif distance <= 12:
        per_times_expense = 4
    elif distance <= 22:
        per_times_expense = 5
    elif distance <= 32:
        per_times_expense = 6
    else:
        #向上取余  多出来的部分,如果小于20km则算1元,
        surplus = distance-32
        if surplus%20 == 0:
            per_times_expense = 6 + surplus//20
        else:
            per_times_expense = 6 + surplus//20 + 1
    #判断是否可以使用交通卡优惠
    if total_expense < 100:
        total_expense += per_times_expense
        print("total:%d   per expense:%d"%(total_expense,per_times_expense))
    elif total_expense < 150:
        total_expense += per_times_expense*0.8
        print("total:%d   per expense:%d"%(total_expense,per_times_expense*0.8))
    else:
        total_expense += per_times_expense*0.5
        print("total:%d   per expense:%d"%(total_expense,per_times_expense*0.5))

7.计算小区反弹

一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

times = 1
hight = 100.0
distance = 0.0
while times <= 10:
    distance += hight
    hight /= 2
    print("times:%d distance:%.2f hight:%.2f"%(times,distance,hight))
    times += 1

8.编写登陆接口

让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序
可以支持多个用户登录 (提示,通过列表存多个账户信息)
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

#使用一个计数器,专门记录同一个用户登录的次数,为三时,用户信息将状态isLock设置为1
#因为这个程序,循环最多执行三次,如果三次全部是同一个用户验证密码,则锁定这个用户
#存储用pickle
import pickle
count = 0
# userinfo_dic = {
#     "xiaoming":{"password":"123","isLock":0},
#     "xiaohua":{"password":"321","isLock":0}
# }
userinfo_dic = {}
login_times = 0
old_name = ""
with open("./userinfo.txt","rb") as  f:
    userinfo_dic = pickle.load(f)
print(userinfo_dic)
while count < 3:
    #用户输入用户名,密码
    username = input("name:").strip()
    psword = input("password:").strip()

    #获取用户信息
    userinfo = userinfo_dic.get(username, False)
    if userinfo:  #用户是否存在
        if not userinfo.get("isLock"):  #状态不为锁定状态
            password = userinfo.get("password")
            if psword == password:
                print("登录成功,欢迎%s"%username)
                break
            else:
                if old_name == username: #判断是否和上次为同一个用户名,是则记录登录次数,否则
                    login_times += 1
                else:
                    old_name = username
                    login_times = 1
                print("密码错误")
        else:
            print("用户已被锁定")
    else:
        print("用户名不存在")
    count += 1
else:
    print("错误次数过多!")
    print(login_times)
    if login_times >= 3:
        userinfo_dic[username]["isLock"] = 1
    with open("./userinfo.txt","wb") as  f:
        pickle.dump(userinfo_dic,f)

猜你喜欢

转载自blog.csdn.net/xgy123xx/article/details/92605501