美文网首页
函数训练

函数训练

作者: 凌航 | 来源:发表于2019-05-25 17:08 被阅读0次

题目1:使用字典表示用户对象,例如:[{‘name’:'zhangsan','pwd':'123456',hasLogin:false}],将字典放入list中来表示数据库,请完成用户 注册、登录功能用户信息。

  1. 注册
  2. 登录
  3. 查看
  4. 退出登录

代码如下:

def register():
    """
    进行注册操作
    :return: None
    """
    name = input("请输入您的账户名:")
    while 1:
        pwd = input("请输入您的账户密码:")
        if len(pwd) >= 6 and len(pwd) <= 9:
            break
        else:
            print("您的输入有误")
    sex = input("请输入您的性别:")
    number = int(input("请输入您的学号:"))

    if not isregisted(name):
        account_information.append({'name':name,'pwd':pwd,'sex':sex, \
        'number':number})
    else:
        print("该用户已存在!")

def isregisted(name):
    """
    判断该账户名是否注册过
    :return: boolean
    """
    for zhanghu in account_information:
        if name == zhanghu['name']:
            return True
    return False

def login():
    """
    登录操作
    :return: 退出登录界面,无返回值
    """
    name = input("请输入您的账户名:")
    pwd = input("请输入您的账户密码:")
    if not isregisted(name):
        print("当前用户不存在")
    elif is_pwd_right(name, pwd):
        print("登录成功!")
        usr_logmenu(name)
        return # 实现退出登录
    else:
        print("您输入的账户名和账户密码不匹配!")

def is_pwd_right(name, pwd):
    """
    判断用户名和密码是否匹配
    :param name: 用户名
    :param pwd: 用户密码
    :return: Boolean
    """
    for i in account_information:
        if name == i['name'] and pwd == i['pwd']:
            return True
    return False

def usr_logmenu(name):
    """
    登录后的菜单
    :return: None
    """
    while 1 :
        print('******************************************')
        print('******************************************')
        print('**                                      **')
        print('**              用户界面                **')
        print('**                                      **')
        print('**        1 查看当前用户信息            **')
        print('**        2 查看用户列表                **')
        print('**        3 退出登录                    **')
        print()
        user_options = int(input("     请输入您要执行的选项的序号:"))
        print('******************************************')
        print('******************************************')
        # print("1 查看当前用户信息")
        # print("2 查看用户列表")
        # print("3 退出登录")
        # user_options = int(input('请输入您要执行的选项的序号:'))
        if user_options == 1:
            see_user_information(name)
        elif user_options == 2:
            see_users()
        elif user_options == 3:
            return
        else:
            print("您的输入有误!")

def see_user_information(name):
    """
    根据用户名查看用户信息
    :param name: 用户名
    :return: None
    """
    for i in account_information:
        if name == i['name']:
            print(i)


def see_users():
    """
    查看有哪些用户
    :return:返回所有用户名称
    """
    for i in account_information:
        print(i['name'],end=' ')
    print()

def select_operat(n):
    """
    主界面操作
    :param n:操作选项
    :return: 无return
    """
    if n == 1:
        register()
    elif n == 2:
        login()
    elif n == 3:
        exit(0) #正常退出程序
    else:
        print("您的输入有误!")

account_information =  \
    [{"name":'李诗才','pwd':'123456','sex':'男','number':20179083}]
def menu():
    while 1:
        print('******************************************')
        print('******************************************')
        print('**                                      **')
        print('**            注册登录界面              **')
        print('**                                      **')
        print('**             (1) 注册                 **')
        print('**             (2) 登录                 **')
        print('**             (3) 退出                 **')
        print()
        n = int(input("      请输入您要执行的功能的序号:"))
        print('******************************************')
        print('******************************************')
        select_operat(n)

menu()

题目2:
模拟用户下单过程,当用户买的物品超过3件时,给与2折优惠(优先级较高)。当用户总价超过100元给与3折优惠。(高阶函数)[{“pname”:"","price":"","number":12}]

代码如下:

products = [{'pname':'笔记本', 'number':2, 'price':2.5},
            {'pname':'手机', 'number':1, 'price':1500}]

def discount(totalPrice, totalNumber):
    if totalNumber > 3:
        return totalPrice * 0.2
    elif totalPrice > 1000:
        return totalPrice * 0.3
    return totalPrice
def app():
    totalNumber = sum([item['number'] for item in products])
    totalprice = sum([item['price'] for item in products])
    print(discount(totalprice, totalNumber))
app()

相关文章

网友评论

      本文标题:函数训练

      本文链接:https://www.haomeiwen.com/subject/nqftzqtx.html