[learn Python from scratch] integrated example 1

Posted by Mercenary on Sat, 01 Feb 2020 16:21:19 +0100

Example: student information management system

'''
    //Student information management system
    1.Student information data source
    2.View student information
    3.Add student information
    4.Delete student information
    5.Exit system
    6.Interface and interaction
'''

# 1. Student information data source
stu_list = [
    {'name':'zhangsan','age':20,'classid':'Python01'},
    {'name':'lisi','age':22,'classid':'Python02'},
    {'name':'wangwu','age':26,'classid':'Python03'}
]

# 2. View student information
def show_info():
    '''

    :return:
    '''
    if(len(stu_list)==0):
        print('='*20,'No student information','='*20)
    else:
        print('|{0:<5}|{1:<10}|{2:<5}|{3:<10}|'.format('sid','name','age','classid'))
        print('-'*40)
        for i,stu_dict in enumerate(stu_list):
            print('|{0:<5}|{1:<10}|{2:<5}|{3:<10}|'.format(i+1,stu_dict['name'],stu_dict['age'],stu_dict['classid']))

# 3. Add student information
def add_stu(name,age,classid):
    stu_dict={}
    stu_dict['name']=name
    stu_dict['age']=age
    stu_dict['classid']=classid
    stu_list.append(stu_dict)

# 4. Delete students
def del_stu(sid):
    sid_int = int(sid)
    stu_list.pop(sid_int)

# 5. Roll out system
def loginOut():
    pass

# 6. Interface and interaction
while True:
    # Output initial interface
    print('='*12,'Student management system','='*12)
    print('{:1}{:13}{:15}'.format(' ','1.View student information','2.Add student information'))
    print('{:1}{:13}{:15}'.format(' ','3.Delete student information','4.Exit system'))
    print('='*36)
    key = input('Please enter the corresponding selection:')
    # Perform the corresponding operation according to the entered value
    if key=='1':
        print('='*12,'Student information browsing','='*12)
        show_info()
        input('Press enter to continue:')
    elif key=='2':
        print('=' * 12, 'Add student information', '=' * 12)
        name = input('Please enter the student's name:')
        age = input('Please enter the age of the student:')
        classid = input('Please enter the student's class number:')
        add_stu(name,age,classid)
        show_info()
        input('Press enter to continue:')
    elif key=='3':
        print('=' * 12, 'Delete student information', '=' * 12)
        show_info()
        sid = input('Please enter the sid: ')
        del_stu(int(sid))
        show_info()
        input('Press enter to continue:')
    elif key=='4':
        loginOut()
        print('=' * 15, 'Bye', '=' * 16)
        break
    else:
        print('Invalid operation!')

Example: bank information management system

According to this idea, we can also design a bank information management system

# 1. Bank user information table

user_lib = [{"id":"11022701","passwd":'123456','balance':100000},
            {"id":'11022702','passwd':'123457','balance':200000},
            {'id':'11022703','passwd':'123458','balance':300000},
            {'id':'1','passwd':'1','balance':300000}]

# 2. Login function

def login_user():
    '''
    @Description:Account authentication
    :return:
    '''

    print('='*12,'Login interface','='*12)
    id = input('Please enter your account number:')
    flag = 0
    for i in user_lib:
        if id == i['id']:
            passwd = input('Please enter your password:')
            if passwd == i['passwd']:
                print('Login succeeded!')
                print('-' * 39)
                flag=1
                user_page2(i['id'])
            else :
                print('Wrong password!')
                print('-' * 39)
                input('Press enter to return to the previous menu')
                login_user()
    if flag==0 :
        print('Account does not exist!')
        print('-' * 39)
        input("Press enter to return to the previous menu.")
        flag=1
# 2. Test the login function
# login_user()

# 3. Exit function
def loginOut():
    print('byebye~')
    pass

# 4. Withdrawal function
def useMoney(user_a):
    for i in user_lib:
        if user_a == i['id']:
            num = int(input('Please enter the amount you want to withdraw:'))
            if i['balance'] >=num:
                i['balance'] = i['balance']-num
                print('Your balance is:',i['balance'],'Yuan!')
                print('-' * 39)
                input('Press enter to return to the previous menu!')
            else :
                print("Your balance is insufficient!")
                print('-' * 39)
                input('Press enter to return to the previous menu!')
# 4. Test withdrawal
# useMoney('11022701')

# 5. Saving function
def saveMoney(user_a):
    for i in user_lib:
        if user_a == i['id']:
            num = int(input('Please enter the amount you want to deposit:'))
            i['balance'] = i['balance']+num
            print('Your balance is:',i['balance'],'Yuan!')
            print('-' * 39)
            input('Press enter to return to the previous menu!')

# 5. Test deposit
# saveMoney('11022701')

# 6. interface 1
def uer_page1():
    while True:
        # Set initial interface
        print('='*12,'Online banking login interface','='*12)
        print('{:1} {:13} {:15}'.format(' ','1.Log in to your account','2.Exit the current interface'))
        print('-'*39)
        key = input('Please enter the corresponding selection:')
        if key == '1' :
            login_user()
        elif key == '2':
            loginOut()
            break

# 6. interface 2
def user_page2(user_id):
    while True:
        # Set initial interface
        print('=' * 12, 'Personal user interface', '=' * 12)
        print('{:1} {:13} {:15}'.format(' ', '1.Online deposit', '2.Online withdrawals'))
        print('{:1} {:13} {:15}'.format(' ', '3.Check the balance', '4.Return'))
        print('-' * 39)
        key = input('Please enter the corresponding selection:')
        if key == '1':
            saveMoney(user_id)
        elif key == '2':
            useMoney(user_id)
        elif key == '3':
            for i in user_lib:
                if user_id == i['id']:
                    print('Your balance is:',i['balance'])
                    print('-' * 39)
                    input('Press enter to return to the previous menu!')
        elif key == '4':
            break
            uer_page1()

uer_page1()
  print('Your balance is:',i['balance'])
                print('-' * 39)
                input('Press enter to return to the previous menu!')
    elif key == '4':
        break
        uer_page1()

uer_page1()

[external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-j7o4aYBR-1580562157441)(C:\Users \ Liu Shengwei \ AppData\Roaming\Typora\typora-user-images\image-20200201210042612.png))
Published 33 original articles, won praise 12, visited 4042
Private letter follow