欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python基础练习题

程序员文章站 2024-02-24 14:09:58
...

一、编写登录接口

输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定

_username="yr"
_password="123"
i=0
for i in range(3):
        username = input("username:")
        password = input("password:")
        if username == _username and password == _password:
            print("恭喜你,登录成功。")
            break
        else:
            if i==2:
                print("账户已锁定请重新验证登录")
            else:
                print("登录失败,请确认用户名或密码")
        i=i+1

测试结果:

输入正确用户名及密码时:
username:yr
password:123
恭喜你,登录成功。


错误时:
username:yy6
password:6
登录失败,请确认用户名或密码
username:7
password:6
登录失败,请确认用户名或密码
username:6
password:6
账户已锁定请重新验证登录

升级版

#Author:Anliu
import json

def login():
    '''提供用户登录接口'''

    _username = input("请输入账号:")
    _password = input("请输入密码:")
    return _username,_password

def user_re():
    '''实现用户注册功能'''

    _username = input("请注册账号:")
    _password = input("请输入初始密码:")
    user = {"user":_username,"passwd":_password}

    with open("passwd","w",encoding=("utf-8")) as f:
        json.dump(user,f)
    f.close()

def check_clock():
    '''实现判断用户账号是否锁定'''

    try:
        with open("passwd","r") as f:
            _user = (json.load(f))
            _username = _user["user"]
            _password = _user["passwd"]
        f.close()
        return _username,_password
    except json.decoder.JSONDecodeError:
        print("您的账号已经被锁定,请联系管理员开通...")

def loading():
    '''实现用户登录'''

    print("logining ...")
    exit()
    return 0

def check_user():
    '''实现用户登录验证成功与否的功能'''

    if _user == user and _passwd == passwd:
        lgm = 0
    elif _user == user or _passwd == passwd:
        lgm = 1
    else:
        lgm = 2
    return lgm

def clock_count():
    '''实现账号锁定功能'''

    with open("passwd", "a") as f:
        f.write("#")
    print("用户账号已锁定...")

def check_count():
    '''实现用户账号登录次数验证'''

    i=0
    while i < 2:
        login()
        if check_user() != 0:
            print("XXXXX")
        i +=1
    clock_count()


_user, _passwd = login()  # 读取用户输入信息
try:
    user, passwd = check_clock()  # 读取原有数据库记录的用户信息
except TypeError:
    exit()
if check_user() == 0:
    print("账号已经存在,请登录 :)")
elif check_user() == 1:
   print("用户名或者密码错误,请再次输入...")
   check_count()
   #_user, _passwd = login()
   #user, passwd = check_clock()
   #if check_user() != 0:
   #    _user, _passwd = login()
   #    user, passwd = check_clock()
   #    if check_user() != 0:
   #        _user, _passwd = login()
   #        user, passwd = check_clock()
   #        if check_user() != 0:
   #            clock_count()
else:
    print("你还没有账号,等毛线啊...注册一个吧!")
    user_re()

二、多级菜单

三级菜单
可依次选择进入各子菜单
所需新知识点,列表,字典

data = {
    '商洛学院':{
        '数计学院':{
            '计科':["ECS","EVS"],
            '网工':["RDS","DBS"]
             },
        '电信学院':{
            '物理':["ECS","EVS"],
            '电信':["RDS","DBS"]
             }
    },
    '西安文理': {
        '数计学院':{
            '计科':["ECS","EVS"],
            '网工':["RDS","DBS"]
             },
        '电信学院':{
            '物理':["ECS","EVS"],
            '电信':["RDS","DBS"]
             }
    },
    '渭南师范': {
        '数计学院':{
            '计科':["ECS","EVS"],
            '网工':["RDS","DBS"]
             },
        '电信学院':{
            '物理':["ECS","EVS"],
            '电信':["RDS","DBS"]
             }
    }
}
while True:
    for i in data:
        print(i)
    choice1 = input("选择进入>>>:")
    if choice1 in data:
        while True:
            for i1 in data[choice1]:
                print("\t",i1)
            choice2 = input("选择进入>>>:")
            if choice2 in data[choice1]:
                while True:
                    for i2 in data[choice1][choice2]:
                        print("\t\t",i2)
                    choice3 = input("选择进入>>>:")
                    if choice3 in data[choice1][choice2]:
                       while True:
                           for i3 in data[choice1][choice2][choice3]:
                              print(i3)
                           choice4 = input("最后一层,按q返回>>:")
                           if choice4 == "q":
                               break
                    if choice3 == "q":
                        break
            if choice2 == "q":
                break

测试结果:

商洛学院
西安文理
渭南师范
选择进入>>>:商洛学院
	 数计学院
	 电信学院
选择进入>>>:数计学院
		 计科
		 网工
选择进入>>>:计科
ECS
EVS
最后一层,按q返回>>:q
		 计科
		 网工
选择进入>>>:q
	 数计学院
	 电信学院

三、购物车程序

#Author:Anliu
product_list = [
    ("Iphone",5800),
    ("Mac Pro",9800),
    ("bike",800),
    ("dabaicai",0.45)
]
choice_list = []
Salary = input("您的余额是:")
if Salary.isdigit():
    Salary = int(Salary)
    while True:
         #for item in product_list:
              #print(product_list.index(item),item)
         for index,item in enumerate(product_list):
              print(index,item)
         user_choice = input("请选择要购买的商品:\n")
         if user_choice.isdigit():
             user_choice = int(user_choice)
             if user_choice < len(product_list) and user_choice >= 0:
                 p_item = product_list[user_choice]
                 if p_item[1] <= Salary:
                     choice_list.append(p_item)
                     Salary -= p_item[1]

                     print("您的商品\033[32;1m%s\033[0m已加入到购物车,您的余额是:\033[31;1m%s\033[0m"%(p_item,Salary))
                 else:
                     print("余额不足....")
         elif user_choice == 'q':
            print("------------------shopping list--------------------")
            for  p in choice_list:
                print(p)
            print("你的余额是:",Salary)
            exit()
         else:
             print("请输入正确的数字...")

测试结果:

您的余额是:10000
0 ('Iphone', 5800)
1 ('Mac Pro', 9800)
2 ('bike', 800)
3 ('dabaicai', 0.45)
请选择要购买的商品:
0
您的商品('Iphone', 5800)已加入到购物车,您的余额是:4200
0 ('Iphone', 5800)
1 ('Mac Pro', 9800)
2 ('bike', 800)
3 ('dabaicai', 0.45)
请选择要购买的商品:
2
您的商品('bike', 800)已加入到购物车,您的余额是:3400
0 ('Iphone', 5800)
1 ('Mac Pro', 9800)
2 ('bike', 800)
3 ('dabaicai', 0.45)
请选择要购买的商品: