学号20213419 刘盼《python程序设计》实验四报告

2022/5/31 1:19:58

本文主要是介绍学号20213419 刘盼《python程序设计》实验四报告,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

课程:《Python程序设计》
班级:2134
姓名:刘盼
学号:20213419
实验教师:王志强
实验日期:2022年5月19日
必修/选修: 公选课

1.实验内容
Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。
2.实验要求
在华为ECS服务器(OpenOuler系统)和物理机(Windows/Linux系统)上使用VIM、PDB、IDLE、Pycharm等工具编程实现。

实验过程及结果
3.1 代码
图书管理器

from audioop import add
from csv import excel
import datetime



book_info=["索引","名称","地址","类别","数量","价格","入库时间","归还时间","次数","状态"]
book_info1=[0,"嫌疑人x的献身","北电院","悬疑",1,35,datetime.datetime(2018,1,1),datetime.datetime(2018,1,1),1,"可借"]
all_book_info=[book_info1]

def add_book(_book_info,mode="a+"):
    f=open(r'lib.txt',mode)
    print(_book_info,file=f)
    f.close()

def renew_books(_all_books):
    for index,_book in enumerate(_all_books):
        if index==0:
            add_book(_book,mode="w+")
        else:
            add_book(_book)

def read_books():
    f=open(r'lib.txt',"r")
    _books_info=f.read()
    _book_info_str=_books_info.split("\n")[:-1]
    _return_info=[]
    for _book in _book_info_str:
        _return_info.append(eval(_book))
    f.close()

    return _return_info


print("1.添加书")
print("2.删除书")
print("3.借书")
print("4.还书")
print("5.查询")
print("6.查询马上要归还的书")
print("7.按照借阅次数排序")
do_now=int(input("what do you want to do?(请输入数字:)"))

if do_now==1:
    print("1.添加书")
    the_add_book=[]
    for info in book_info:
        if info in ["索引","数量","价格","次数"]:
            put_number=int(input(info +":"))
            the_add_book.append(put_number)
        elif info in ["入库时间","归还时间"]:
            put_date=input(info+":(用英文逗号分开年月日)").split(",")
            the_add_book.append(datetime.datetime(int(put_date[0]),int(put_date[1]),int(put_date[2])))
        else:
            put_txt=input(info+":")
            the_add_book.append(put_txt)
    add_book(the_add_book)

elif do_now==2:
    print("2.删除书")
    all_book_info=read_books()
    for _book in all_book_info:
        print(_book[1])
    del_book_name=input("which book do you want to cut?:")
    stay_book=[]
    for _book in all_book_info:
        if del_book_name !=_book[1]:
            stay_book.append(_book)
    renew_books(stay_book)

elif do_now==3:
    all_book_info=read_books()
    for _book in all_book_info:
        if "不"not in _book[-1]:
            print(_book[1])
    borrow_book_name=input("which book do you want to borrow?:")        
    for _book in all_book_info:
        if borrow_book_name==_book[1]:
            _book[4]=_book[4]-1
            _book[-2]=_book[-2]+1
            if _book[4]==0:
                _book[-1]="不可借"

    renew_books(all_book_info)

elif do_now==4:
    all_book_info=read_books()
    for _book in all_book_info:
            print(_book[1])
    return_book_name=input("which  book do you want to return?:")        
    for _book in all_book_info:
        if return_book_name==_book[1]:
            _book[4]=_book[4]+1
            _book[-2]=_book[-2]-1
            if _book[4]==1:
                _book[-1]="可借"

    renew_books(all_book_info)

elif do_now==5:
    all_book_info=read_books()
    for _book in all_book_info:
            print(_book[1])
    search_book_name=input("which  book do you want to skim?:")        
    for _book in all_book_info:
        if search_book_name==_book[1]:
            print(_book)

    renew_books(all_book_info)

elif do_now==6:
    all_book_info=read_books()
    for _book in all_book_info:
        if datetime.datetime.now()-_book[-3]>datetime.timedelta(days=3):
           print(_book[1],datetime.datetime.now()-_book[-3])
    

elif do_now==7:
    all_book_info=read_books()
    all_book_info.sort(key=lambda x:x[-2] * -1)
    for _book in all_book_info:
        print(_book[1])

将图书分配到excel表格

import pandas as pd
import datetime

excel_path= "lib.xlsx"

book_info=["索引","名称","地址","类别","数量","价格","入库时间","归还时间","次数","状态"]
book_info1=[0,"嫌疑人x的献身","北电院","悬疑",1,35,datetime.datetime(2018,1,1),datetime.datetime(2018,1,1),1,"可借"]
all_book_info=[book_info,book_info1]


def write_excel(input_info):
    all_book_info_pd=pd.DataFrame(input_info)
    all_book_info_pd.to_excel(excel_path)
    print("all is write")


def read_excel():
    df =pd.read_excel(excel_path)
    excel_read=[]
    for _excel_index in range(len(df)):
        excel_read_one=[]
        for _index,_date_index in enumerate(df.iloc[_excel_index,:]):
            if _index !=0:
                excel_read_one.append(_date_index)
        excel_read.append(excel_read_one)

    return excel_read

界面设置

import datetime

import tkinter as tk

from excel import read_excel,write_excel

def apple():
    a=1

class MainPage:
    def bf_go_add(self):
        self.root.destroy()
        AddBook()
    def bf_go_del(self):
        self.root.destroy()
        DelBook()
    def bf_go_borrow(self):
        self.root.destroy()
        BorrowBook()
    def bf_go_return(self):
        self.root.destroy()
        ReturnBook()
    def bf_go_find(self):
        self.root.destroy()
        FindBook()
    def bf_go_delay(self):
        self.root.destroy()
        DelayBook()
    def bf_go_popular(self):
        self.root.destroy()
        PopularBook()

    def __init__(self):
        self.root=tk.Tk()
        tk.Button(self.root,text="添加书",command=self.bf_go_add).grid(row=0,column=0)
        tk.Button(self.root,text="删除书",command=self.bf_go_del).grid(row=1,column=0)
        tk.Button(self.root,text="借书",command=self.bf_go_borrow).grid(row=2,column=0)
        tk.Button(self.root,text="还书",command=self.bf_go_return).grid(row=3,column=0)
        tk.Button(self.root,text="查询图书信息",command=self.bf_go_find).grid(row=4,column=0)
        tk.Button(self.root,text="查询要归还的书",command=self.bf_go_delay).grid(row=5,column=0)
        tk.Button(self.root,text="按照借阅次数查询",command=self.bf_go_popular).grid(row=6,column=0)

        self.root.mainloop()

class SmallPage(object):
    def __init__(self):
        self.root=tk.Tk()
        tk.Button(self.root,text="返回",command=self.bf_go_main).grid(row=10,column=0)


    def bf_go_main(self):
        self.root.destroy()
        MainPage()

class AddBook(SmallPage):
    def bf_add_book(self):
        all_book_info=read_excel()
        
        book_index=int(self.a_index.get())
        book_name=self.a_name.get()
        book_address=self.a_address.get()
        book_class=self.a_class.get()
        book_number=int(self.a_number.get())
        book_pay=int(self.a_pay.get())
        book_time_in_1=self.a_time_in.get().split(",")
        book_time_in=datetime.datetime(int(book_time_in_1[0]),int(book_time_in_1[1]),int(book_time_in_1[2]))
        book_time_out_1=self.a_time_out.get().split(",")
        book_time_out=datetime.datetime(int(book_time_out_1[0]),int(book_time_out_1[1]),int(book_time_out_1[2]))
        book_time=int(self.a_time.get())
        book_state=self.a_state.get()

        all_book_info.append([book_index,book_name,book_address,book_class,book_number,book_pay,book_time_in,book_time_out,book_time,book_state])

        write_excel(all_book_info)
    

    def __init__(self):
        super().__init__()
        tk.Label(self.root,text = "索引:").grid(row=0,column=0)
        self.a_index=tk.Entry(self.root)
        self.a_index.grid(row=0,column=1)
        tk.Label(self.root,text = "名称:").grid(row=1,column=0)
        self.a_name=tk.Entry(self.root)
        self.a_name.grid(row=1,column=1)
        tk.Label(self.root,text = "地址:").grid(row=2,column=0)
        self.a_address=tk.Entry(self.root)
        self.a_address.grid(row=2,column=1)
        tk.Label(self.root,text = "类别:").grid(row=3,column=0)
        self.a_class=tk.Entry(self.root)
        self.a_class.grid(row=3,column=1)
        tk.Label(self.root,text = "数量:").grid(row=4,column=0)
        self.a_number=tk.Entry(self.root)
        self.a_number.grid(row=4,column=1)
        tk.Label(self.root,text = "价格:").grid(row=5,column=0)
        self.a_pay=tk.Entry(self.root)
        self.a_pay.grid(row=5,column=1)
        tk.Label(self.root,text = "入库时间:").grid(row=6,column=0)
        self.a_time_in=tk.Entry(self.root)
        self.a_time_in.grid(row=6,column=1)
        tk.Label(self.root,text = "归还时间:").grid(row=7,column=0)
        self.a_time_out=tk.Entry(self.root)
        self.a_time_out.grid(row=7,column=1)
        tk.Label(self.root,text = "次数:").grid(row=8,column=0)
        self.a_time=tk.Entry(self.root)
        self.a_time.grid(row=8,column=1)
        tk.Label(self.root,text = "状态:").grid(row=9,column=0)
        self.a_state=tk.Entry(self.root)
        self.a_state.grid(row=9,column=1)
        tk.Button(self.root,text="添加",command=self.bf_add_book).grid(row=10,column=0)

        self.root.mainloop()

class DelBook(SmallPage):
    def bf_del_book(self):
        stay_book=[]
        for _book in self.all_book_info:
            if self.a_del.get() !=_book[1]:
                stay_book.append(_book)
        write_excel(stay_book)
    def __init__(self):
        super().__init__()
        self.all_book_info=read_excel()
        self.all_book_info_1=self.all_book_info[1:]
        for _index,_book in enumerate(self.all_book_info_1):
            tk.Label(self.root,text=_book[1]).grid(row=_index,column=0)
        tk.Label(self.root,text="请输入你想删除的书").grid(row=len(self.all_book_info_1),column=0)
        self.a_del=tk.Entry(self.root)
        self.a_del.grid(row=len(self.all_book_info_1)+1,column=1)
        tk.Button(self.root,text="删除",command=self.bf_del_book).grid(row=10,column=0)
   
class BorrowBook(SmallPage):
    def bf_borrow_book(self):
        
        for _book in self.all_book_info:
            if self.a_borrow.get() ==_book[1]:
                _book[4]=_book[4]-1
                _book[-2]=_book[-2]+1
                if _book[4]==0:
                   _book[-1]="不可借"
        write_excel(self.all_book_info)
    def __init__(self):
        super().__init__()
        self.all_book_info=read_excel()
        self.all_book_info_1=self.all_book_info[1:]
        for _index,_book in enumerate(self.all_book_info_1):
            if "不"not in _book[-1]:
                tk.Label(self.root,text=_book[1]).grid(row=_index,column=0)
        tk.Label(self.root,text="请输入你想借阅的书").grid(row=len(self.all_book_info_1),column=0)
        self.a_borrow=tk.Entry(self.root)
        self.a_borrow.grid(row=len(self.all_book_info_1)+1,column=1)
        tk.Button(self.root,text="借阅",command=self.bf_borrow_book).grid(row=10,column=0)

class ReturnBook(SmallPage):
    def bf_return_book(self):
        
        for _book in self.all_book_info:
            if self.a_return.get() ==_book[1]:
                _book[4]=_book[4]+1
                if _book[4]==1:
                   _book[-1]="可借"
        write_excel(self.all_book_info)
    def __init__(self):
        super().__init__()
        self.all_book_info=read_excel()
        self.all_book_info_1=self.all_book_info[1:]
        for _index,_book in enumerate(self.all_book_info_1):
            tk.Label(self.root,text=_book[1]).grid(row=_index,column=0)
        tk.Label(self.root,text="请输入你想还的书").grid(row=len(self.all_book_info_1),column=0)
        self.a_return=tk.Entry(self.root)
        self.a_return.grid(row=len(self.all_book_info_1)+1,column=1)
        tk.Button(self.root,text="还书",command=self.bf_return_book).grid(row=10,column=0)



class FindBook(SmallPage):
    def bf_find_book(self):
        for _book in self.all_book_info:
            if self.a_find.get() ==_book[1]:
               tk.Label(self.root,text=_book).grid(row=len(self.all_book_info_1)+2,column=0)
        
    def __init__(self):
        super().__init__()
        self.all_book_info=read_excel()
        self.all_book_info_1=self.all_book_info[1:]
        for _index,_book in enumerate(self.all_book_info_1):
            tk.Label(self.root,text=_book[1]).grid(row=_index,column=0)
        tk.Label(self.root,text="请输入你想查询的书").grid(row=len(self.all_book_info_1),column=0)
        self.a_find=tk.Entry(self.root)
        self.a_find.grid(row=len(self.all_book_info_1)+1,column=1)
        tk.Button(self.root,text="查询",command=self.bf_find_book).grid(row=10,column=0)        


class DelayBook(SmallPage):
    
    def __init__(self):
        super().__init__()
        self.all_book_info=read_excel()
        self.all_book_info_1=self.all_book_info[1:]
        for _index,_book in enumerate(self.all_book_info_1):
            if datetime.datetime.now()-_book[-3]>datetime.timedelta(days=2000):
                tk.Label(self.root,text=_book[1]).grid(row=_index,column=0)
       
class PopularBook(SmallPage):
    
    def __init__(self):
        super().__init__()
        self.all_book_info=read_excel()
        self.all_book_info_1=self.all_book_info[1:]
        self.all_book_info_1.sort(key=lambda x:x[-2] * -1)
        for _index,_book in enumerate(self.all_book_info_1):
            tk.Label(self.root,text=_book[1]).grid(row=_index,column=0)

MainPage()

3.2运行结果


4.实验过程中遇到的问题和解决过程
1.问题:在编写时要十分重要的思维逻辑和记忆能力才能记住长代码,否则容易出错
解决方案:程序编写一段就运行,保证不错才继续进行。
2.问题:在excel的使用中要写表格,引用了pandas函数,但pandas函数没有被定义
解决方案:在网页中收缩资料查找如何安装pandas,在同学的帮助下成果安装pandas
3.问题:界面编程中无法找到界面
解决方案:引用tkinter函数,可以获得一个小界面,可以使用代码在界面上添加一些功能和点缀
4.问题:在编写过程中我多个功能用了同一个名字,导致后面编写代码的时候分不清谁是谁
解决方案:按代码的功能定义名字,更方便我们使用功能
5.感悟
1.在图书管理系统编译中有很多我不懂知识,例如datetime,tkinter的使用方法,这些都是我未曾接触的领域,所以自己在网站上搜查资料和看相关视频,并且不断叨扰同学才能略微懂得使用方法,这个学习过程不是特别顺利,也让我感觉到有些苦闷,但当最后代码能够成功运行时,我真的感到十分激动,那是认真付出获得收获后的喜悦,难于形容。
2.在一开始老师布置这个实验的时候,我感觉到了无比的压力,一方面是因为我一个大一新生对python不是特别精通,不知道能不能完成老师的要求,另一方面是我是通讯工程,对计算机的学习也不是特别多,所以感到压力很大,但在实验的过程中,我慢慢领会了老师的意思,我觉得这个实验就是来培养我们对python的兴趣(这也是大部分同学选择游戏的原因),让我们在实验的过程中不断去学习新的东西,培养我们写代码正确的习惯(譬如:写注释,尽量缩减代码,不懂得要主动学习)
3.这次实验不是我一个人的功劳,感谢我的室友帮助和网友援助,我也在这一过程中收获到了不少的知识和先苦后甜的一种喜悦,也让我明白了自身的不足,为今后更好的学习埋下基础

最后,python也结课了,还记得当时报python课的情景,报python的具体原因已经不清楚了,只是认为多学一点编程语言对我有好处,而且室友都想报python,大家就一起报了。python是很受欢迎的一门语言,我觉得自己十分幸运,能够抢到python这门课。
感谢老师的教导,让我在python中学习了许多知识,如基本的语法结构,正则表达式,继承,多态,封装......等等,这些都让我受益匪浅,今后我也会不断地去学习python,因为我认为大二大三乃至今后做许多事肯定会用到python这一门语言,毕竟python排名第一,而且“人生苦短,我用python”不是凭空如来的
然后简单总结下这学期的学习内容:
(1)变量类型
(2)运算操作符
(3)数据类型
(4)循环,判断语句
(5)列表、元组、字典、集合的使用
(6)正则表达式
(7)面向对象
(8)文件操作及异常处理
(9)爬虫
(10)CSDN的学习与测评
最后,再次向老师表达感谢!



这篇关于学号20213419 刘盼《python程序设计》实验四报告的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程