day05 python multilayer adorners, modules, string formatting, generators and iterators, recursion

Source: Internet
Author: User

One, multi-layer decorative device

The requirements are as follows:

Run the program to determine whether there is currently a user login, and the current login is the user name is an administrator account, but some functions only need to determine whether there is currently a user login can, and some function two needs to be judged, so the two judgments are divided into two adorners, Use the appropriate one or two adorners directly when you need to determine the code as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-#Save the current login account name and account type (admin is 2, normal user is 1)User_info = {}defCheck_login (func):"""The adorner that determines whether a user is currently logged on:p Aram Func:: Return:"""    defInner (*args, * *Kwargs):ifUser_info.get ('Is_login', None): Ret= Func (*args, * *Kwargs)returnretElse:            return('Please login')    returnInnerdefCheck_admin (func):"""Adorner: Used to determine whether the type of user currently logged in is a normal account or an administrator account:p Aram Func:: Return:"""    defInner (*args, * *Kwargs):ifUser_info.get ('User_type', None) = = 2: Ret= Func (*args, * *Kwargs)returnretElse:            return('no permission to view')    returnInnerdeflogin ():"""Login function: return:"""username= Input ("Please enter user name:") Password= Input ("Please enter your password:")    ifUsername = ='Ordinary'  andPassword = ='123': user_info['Is_login'] =True user_info['User_type'] = 1return("Welcome to visit")    elifUsername = ='Admin'  andPassword = ='456': user_info['Is_login'] =True user_info['User_type'] = 2return("Welcome Administrator Login")    Else:        return "incorrect user name or password"@check_login @check_admindefindex_admin ():"""The functionality of the administrator needs to determine whether a user is currently logged on, and whether the currently logged on user is an administrator account, so there are two adorners: return:"""    return('features of the Administrator') @check_logindefindex_ordinary ():"""functions of the general account: return:"""    return "features for ordinary users"defrun ():"""Front End: return:"""Dict_choice= {'1': Login,'2': Index_admin,'3': Index_ordinary} whileTrue:Print("1: Login \n2: admin function \n3: normal user function") User_choice= Input ("Please enter the project number (enter ' Q ' to end the program):")        ifUser_choiceinchDict_choice.keys (): Res=Dict_choice[user_choice] ()Print(RES)elifUser_choice = ='Q':             Break        Else:            Print("your input is wrong, please re-enter")if __name__=='__main__': Run ()

Two-layer adorners perform plots:

day05 python multilayer adorners, modules, string formatting, generators and iterators, recursion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.