python_August(函数对象、功能选择)

yyds703 / 2024-09-05 / 原文

目录
  • python中一切皆对象
  • 功能选择
  • 函数的嵌套
  • 功能选择增加内容版

python中一切皆对象

# python中一切皆对象
# print(self_max)  # <function self_max at 0x0000020E4456CF28>
# print(id(self_max))
# print(type(self_max))
# print(type([1,2]))
# print(type(1))
# print(type(True))

# 1. 引用,赋值
# a = 'str'
# 'str'.strip()
# a.strip()

# 2. 当做函数的返回值
# def f2():
#     print('from f2')
#
# def f1():
#     return f2  # f2 =1
#
# print(f1())  # f2  <function f2 at 0x0000020E4456CF28>
# f = f1()   # f = f2
# f()  # f2()

# 3. 当做函数的参数

# def f1():
#     print('from f1')

# def f2(m):  # m=f1   # m ='str'
#     return m # m=f1  # m= 'str'
#
# f = f2(f1)  # f1
# f()   # f1()
#
# s = f2('str')  # 'str'
# print(s)

# 4. 作为容器元素的元素
# def f1():
#     print('from f1')
#
#
# l = ['str', 1, f1]
# l[2]()

功能选择

def trans():
    print('from trans')


def withdraw():
    print('from withdraw')


func_dict_print = {
    0: 'trans',
    1: 'withdraw',
    2: 'quit',
}

func_dict = {
    0: trans,
    1: withdraw,
}

while True:
    print(func_dict_print)
    choice = input('请选择你想要的选择的功能>>>').strip()
    choice_int = int(choice)

    if choice_int == 2:
        break

    func_dict[choice_int]()

函数的嵌套

 if 1:
#     if 2:
#         pass

# 定义阶段只检测语法,不执行代码
# def f1():
#     def f2():
#         print('from f1')
#     f2()
#
# f1()

功能选择增加内容版

def register():
    """注册"""
    # 判断是否注册成功
    is_register = False

    # 输入用户名密码
    username = input("请输入你的用户名>>>").strip()
    pwd = input("请输入你的密码").strip()

    # 保存用户信息
    with open('user_info.txt', 'a', encoding='utf8') as fr:
        fr.write(f'{username}:{pwd}')
        is_register = True

    return is_register


def login():
    """登录"""
    # 判断是否登陆成功
    is_login = False

    # 输入用户名和密码
    username = input("请输入你的用户名>>>").strip()
    pwd = input("请输入你的密码").strip()

    # 用户名和密码比对
    with open('user_info.txt', 'r', encoding='utf8') as fr:
        # 循环遍历所有用户
        for user in fr:
            user_list = user.strip().split(':')
            username_f = user_list[0]
            pwd_f = user_list[1]

            # 判断是否有比对成功用户
            if username == username_f and pwd == pwd_f:
                is_login = True
                print('登陆成功')
                return is_login

        return is_login



def shopping():
    """购物"""
    # 判断是否成功购物
    is_shopping = False

    # 定义商品
    goods_dict = {
        0: 'apple',
        1: 'tesla',
        2: 'maikailun',
        3: 'ballwawa',
    }

    # 打印商品信息
    for goods in goods_dict.items():
        print(goods)

    # 用户输入想要购买的商品
    goods_choice = input("请选择你想要购买的商品>>>").strip()

    # 判断用户输入是否符合规则
    if not goods_choice.isdigit():
        print('傻逼,重新选择')
        return is_shopping

    # 成功购物
    print(f'你已经成功购买{goods_dict[int(goods_choice)]}')
    is_shopping = True

    return is_shopping


func_dict = {
    0: register,
    1: login,
    2: shopping,
}
msg = '''
0 : register 注册
1 : login 登录
2 : shopping 购物
3 : quit 退出
'''

while True:
    # 打印功能
    print(msg)

    # 输入功能
    func_choice = input("请选择你想要输入的功能:>>>").strip()

    # 判断是否误输入
    if func_choice.isdigit():
        func_choice = int(func_choice)

    # 判断是否退出
    if func_choice == 3:
        break

    # 判断是否继续
    func_res = func_dict[func_choice]()
    if func_res:
        continue_choice = input('是否继续,继续请输入"y"or"Y">>>')

        if not (continue_choice == 'y' or continue_choice == 'Y'):
            print('好吧,谢谢你关顾nick大厦')
            break