首页  >  问答  >  正文

python下 如何科学设计一个命令列表执行对应流程?

PHPzPHPz2765 天前662

全部回复(2)我来回复

  • 天蓬老师

    天蓬老师2017-04-18 10:03:36

    同楼上, 用dic

    def get_option():
        return "逗你玩"
    
    
    def find_score():
        pass
    
    
    def add_score():
        pass
    
    
    def sub_tract_score():
        pass
    
    
    def kic():
        pass
    
    
    def blacklist():
        pass
    
    
    def main(op):
        op_dic = {
            "查积分": find_score,
            "加积分": add_score,
            "减积分": sub_tract_score,
            "踢人": kic,
            "黑名单": blacklist,
        }
        op = op_dic.get(op)
        if op:
            return op()
        else:
            print("查无此令")
            
            
    if __name__ == "__main__":
        option = get_option()
        main(option)
    

    回复
    0
  • PHP中文网

    PHP中文网2017-04-18 10:03:36

    用 dict ?

    actions_d = {
    '查积分': findScore,
    '加积分': addScore,
    '减积分': subtractScore,
    '踢人': kick,
    '设置谁为黑名单': blacklist,
    }
    actions_d.get(action, default_action)()

    回复
    0
  • 取消回复