Course design student information management system python (character, list, dictionary, modular complete operation)

Posted by vMan on Thu, 23 Dec 2021 09:35:43 +0100

Primary comprehensive application of python -- student information management system

Variable process control function module

Project requirements:

Realize the addition, deletion, modification, query and exit of the business card. Select different functions by selecting different numbers. The user's name, telephone, QQ email can be modified if the user queries the specified business card, and use modularization for development.

Steps:

1. Frame construction

2. Add business card

3. Display all business cards

4. Query business card

5. Modify or delete the business card after successful query

6. Let python run directly

1. Frame construction

Prepare files, determine file names, write code where needed, write topic loops, and realize basic user input and judgment

New card_main.py saves the main program function and the entry card of the program_ tools. Py encapsulates the functions of adding, querying, modifying and deleting business cards in different functions.

Idea: set the cycle function in the main program, and keep the cycle running if you don't actively exit. Infinite loop

Write main run cycle (cards_main.py)

#! usr/bin/python3
import cards_tools
#Infinite loop, operated by the user, whether to end the loop
while True:
    #TODO (Mr_Liang) displays the function menu, which can remind us in time
    cards_tools.show_menu()
    action_str =input("Please enter the action you want to perform")
    print("The action you performed is[%s]"%action_str)
    if action_str in ["1","2","3"]:
        #Add business card
        if action_str =="1":
            cards_tools.new_card()
        #Show all
        elif action_str =="2":
            cards_tools.show_all()
        #Query business card
        elif action_str =="3":
            cards_tools.search_card()
    elif action_str in ["0"]:
    # elif action_str =="0":
        print("Welcome to [business card management system] again!")
        break
    else:
        print("Input error!")
#if action in ["1","2","3"]:
# You can also use this statement to judge if action = = "1 or action = =" 2 "or action = =" 3“
#Note: we must not use int to receive here. If it is not int type, an error will be reported, resulting in the program being unable to execute

Function function (cards_tools.py)

#The list of all business card records. The list whose initialization list is empty is used to store information
card_list =[]
def show_menu():
    print("*" * 50)
    print("Welcome to [business card management system] V1.0")
    print("")
    print("1.New business card")
    print("2.Show all")
    print("3.Search business card")
    print("")
    print("0.Exit the system!")
    print("*" * 50)
def new_card():
    """Add business card"""
    print("-"*50)
    print("Add business card")
    #1. Prompt the user to enter the details of the business card
    #Select the variable you want to rename in pycharm. shift+F6 refactoring code can be renamed quickly
    name_str =input("Please enter your name:")
    phone_str= input("Please enter phone number:")
    qq_str =input("Please enter QQ: ")
    Email_str= input("Please enter email address:")
    #The name in front is the key of the index, followed by the received value
    #2. Create a business card dictionary using the information entered by the user
    card_dict = {"name": name_str, "phone": phone_str, "qq": qq_str, "email": Email_str}
    #3. Add the business card dictionary to the list
    card_list.append(card_dict)
    print(card_list)
    #4. User information added successfully
    print("add to %s Business card success"%name_str)
def show_all():
    """Show all business cards"""
    print("-"*50)
    print("Show all business cards")
    #Judge whether there is a business card record. If not, prompt the user to return
    if len(card_list) ==0:
        print("There is no business card record at present. Please use the new function to add a business card!")
        #return returns the execution result of a function. The code below will not be executed
        #If there is nothing after return, it will return to the location of the calling function without returning any results
        return
    #Print header
    for name in ["full name","Telephone","QQ","mailbox"]:
        print(name,end="\t\t\t")
    print("")
    #Print split lines
    print("="*50)
    #Traverse the business card list and output dictionary information in turn
    for card_dict in card_list:
        print("%s\t\t%s\t\t%s\t\t%s"%(card_dict["name"],card_dict["phone"],card_dict["qq"],card_dict["email"]))
def search_card():
    """Search business card"""
    print("-"*50)
    print("Search business card")
    #1. Prompt the user for the name to search
    find_name = input("Please enter a name to search for:")
    #2. Traverse the business card list and query the name to be searched. If it is not found, the user needs to be prompted. If it is not found, you can use for and else:
    for card_dict in card_list:
        if card_dict["name"] ==find_name:
            print("eureka")
            print("full name\t\t Telephone\t\tQQ\t\t mailbox")
            print("="*50)
            print("%s\t\t%s\t\t%s\t\t%s" % (card_dict["name"], card_dict["phone"], card_dict["qq"], card_dict["email"]))
            #Modify and delete the business card records found
            deal_card(card_dict)
            break
    else:
        print("Sorry, not found%s"%find_name)
def deal_card(find_dict):
    print(find_dict)
    action_str =input("Please enter the action to be performed 1 modify 2 delete 0 return to superior")
    if action_str =="1":
        find_dict["name"] =input_card_info(find_dict["name"], "full name:")
        find_dict["phone"] =input_card_info(find_dict["phone"],"Telephone:")
        find_dict["qq"] =input_card_info(find_dict["qq"],"QQ: ")
        find_dict["email"] =input_card_info(find_dict["email"],"Email: ")
        print("Business card modified successfully!")
    elif action_str =="2":
        card_list.remove(find_dict)
        print("Business card deleted successfully!")

#For null value processing
def input_card_info(dict_value,tip_message):
    """
    :dict_value: Dictionary original value
    :tip_message: Prompt text entered
    :return: If the user enters the content, the content is returned; otherwise, the original value of the dictionary is returned
    """

    #1 prompt the user for input
    result_str =input(tip_message)
    #2. Judge the user's input. If the user inputs content, the result will be returned directly
    if len(result_str)>0:
        return result_str
    # 3. If the user does not enter the content, return the original value of the dictionary
    else:
        return dict_value

shebang tag under Linux

To use Shebang

1. Use which to query python3

$which python3

2. Modify the python master file to run, and add the following content in the first line

#!/usr/bin/python3

3. Modify the file permissions of the master file and increase the execution permissions

$chmod +x cards_main.py

4. Execute the procedure when necessary

./cards_main.py


In fact, personally, I prefer to compile and develop under Ubuntu, which can greatly improve the development efficiency.
Looking back on the whole project, it is not difficult to find that modularization can make our code more readable and executable, and add functions to make the code more regular. The whole project spent a morning from the beginning to the end, which is my first understanding of python. When my code ability has made rapid progress since I learned c + + and PHP, I would like to call it the strongest development language on the surface!!!!

Topics: Python Linux Ubuntu Algorithm data structure