Python基础语法毕业笔记-最简单的添加删除程序

学了接近2天Python基础语法,感觉差不多了可以去看Python源码了,做一个基础语法毕业的小程序,

 

程序运行截图如下:

Python基础语法毕业笔记-最简单的添加删除程序_第1张图片

Python基础语法毕业笔记-最简单的添加删除程序_第2张图片

Python基础语法毕业笔记-最简单的添加删除程序_第3张图片

对应的文本文件如下:

Python基础语法毕业笔记-最简单的添加删除程序_第4张图片

程序结构如下:

Python基础语法毕业笔记-最简单的添加删除程序_第5张图片

源码如下:

File.py

from Student import Student

#p=Student("XiaoBai","Male",20,"110","NanJing")

def ReadExtraFile(peopleList):
    f=open("people.txt","r")
    for line in f:
        line=line.replace("\n","")
        list=line.split(" ")
        peopleList.append(Student(list[0],list[1],int(list[2]),list[3],list[4]))
        pass
    f.close()
    pass



def wirteToText(peopleList,FileName):
    f=open(FileName,"w")
    for line in peopleList:
        outPutStr=line.m_stuName+" "+line.m_stuSex+" "+str(line.m_stuAge)+" "+line.m_phoneNum+" "+line.getAddress()+"\n";
        f.write(outPutStr)
        pass
    f.close()
    

Student.py

class Student:
    m_stuName="NULL"
    m_stuSex="Male"
    m_stuAge=18
    m_phoneNum="NULL"
    
    __m_address__="NanJing"
    
    def __init__(self,stuName,stuSex,stuAge,phoneNum,address):
        self.m_stuName=stuName
        self.m_stuSex=stuSex
        self.m_stuAge=stuAge
        self.m_phoneNum=phoneNum
        self.__m_address__=address
        pass
    
    def getAddress(self):
        return self.__m_address__
    
    

WidgetShell.py

from Student import Student
import os
import time
import sys
from File import ReadExtraFile,wirteToText


studentList=[]

#程序运行开始首先加载文件
ReadExtraFile(studentList)

#p=Student("XiaoBai","Male",20,"110","NanJing")


def ReadDataFile():
    for pointer in studentList:
        print("姓名:%s  性别:%s  年龄:%d  电话号码:%s  住址:%s"%(pointer.m_stuName,pointer.m_stuSex,pointer.m_stuAge,pointer.m_phoneNum,pointer.getAddress()))
        pass
    
    print("等待返回!")
    time.sleep(3)
    os.system("cls")
    pass

def AddMemer():
    nameStr=input("请输入姓名:")
    maleStr=input("请输入性别:")
    ageStr=input("请输入年龄:")
    phoneStr=input("请输入电话号码:")
    addressStr=input("请输入地址:")
    if(ageStr.isdigit()==False):
        print("年龄输入有误!添加失败")
        time.sleep(1)
        os.system("cls")
        return
    
    studentList.append(Student(nameStr,maleStr,int(ageStr),phoneStr,addressStr))
    print("等待返回!")
    time.sleep(3)
    os.system("cls")
    pass

def WriteDataToFile():
    wirteToText(studentList,"people.txt")
    print("写入数据成功!等待返回")
    time.sleep(3)
    os.system("cls")

if __name__=="__main__":
    while(True):
        print("------------------------------添加删除Shell---------------------------")
        print("1.打印所有数据")
        print("2.添加成员")
        print("3.写入到数据文件")
        print("4.退出程序")
        print("-----------------------------------------------------------------------")
        InputKeyboard=input("请输入选项:");
        
        if(InputKeyboard.isdigit()==False):
            print("输入有误请重新输入")
            time.sleep(1)
            os.system("cls")
            continue
            pass
        
        InputNum=int(InputKeyboard)
        if(InputNum==1):
            ReadDataFile()
            pass
        elif(InputNum==2):
            AddMemer()
            pass
        elif(InputNum==3):
            WriteDataToFile()
            pass
        elif(InputNum==4):
            sys.exit();
        else:
            print("输入错误请重写输入!")
            time.sleep(2000)
            os.system("cls")
    

 

你可能感兴趣的:(Python,Python)