Python函数

# Author: CCH
# -*-  coding = utf-8 -*-
# @Time : 2020/10/27 8:27
# @File : python函数.py
# @Software : PyCharm

# 函数对象的应用

def login():
    print("登录功能")


def transfer():
    print("转账功能")


def check_banlance():
    print("查询余额")


def withdraw():
    print("提现")


def register():
    print("注册")


func_dict = {
    "1": login,
    "2": transfer,
    "3": check_banlance,
    "4": withdraw,
    "5": register
}

while True:
    print("""
    0 退出
    1 登录
    2 转账
    3 查询余额
    4 提现
    5 注册
    """)

    choice = input("请输入命令编号:").strip()
    if not choice.isdigit():
        print("必须输入编号")
        continue

    if choice == "0":
        break

    if choice in func_dict:
        func_dict[choice]()

    else:
        print("输入的指令不存在")

修正版

# Author: CCH
# -*-  coding = utf-8 -*-
# @Time : 2020/10/27 8:38
# @File : python函数修正版.py
# @Software : PyCharm


# 函数对象的应用

def login():
    print("登录功能")


def transfer():
    print("转账功能")


def check_banlance():
    print("查询余额")


def withdraw():
    print("提现")


def register():
    print("注册")


func_dict = {
    "0": ("退出", None),
    "1": ("登录", login),
    "2": ("转账", transfer),
    "3": ("查询余额", check_banlance),
    "4": ("提现", withdraw),
    "5": ("注册", register)
}

while True:
    for k in func_dict:
        print(k, func_dict[k][0])

    choice = input("请输入命令编号:").strip()
    if not choice.isdigit():
        print("必须输入编号")
        continue

    if choice == "0":
        break

    if choice in func_dict:
        func_dict[choice][1]()

    else:
        print("输入的指令不存在")
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页