实验一 python 编程基础

import math


def estimated_people(scale):
    while True:
        work_hours = float(input("请输入工时大小:(可以输入小数) "))
        if work_hours <= 0:
            print("输入错误,请重新输入")
            continue
        hours = scale * 80
        worker_amount = math.ceil(hours / work_hours)
        print(f"项目大小为{scale}个标准项目,如果需要再{work_hours}个工时完成,则需要的人力数量为:{worker_amount}人")
        break


def estimated_time(scale):
    while True:
        worker_amount = int(input("请输入人力数量:(请输入整数) "))
        if worker_amount <= 0:
            print("输入错误,请重新输入")
            continue
        hours = scale * 80
        time = hours / worker_amount
        print(f"项目大小为{scale}个标准项目,使用{worker_amount}个人力完成,", end="")
        print("则需要工时数量为:%.1f个" % time)
        break


def main():
    while True:
        Type = input("请选择计算类型:(1-人力计算,2-工时计算) ")
        scale = float(input("请输入项目大小:(1代表标准大小,可以输入小数) "))
        if Type == "1":
            estimated_people(scale)
            break
        elif Type == "2":
            estimated_time(scale)
            break
        else:
            print("输入有误,请重新输入")
            continue


print("欢迎使用工作量计算小程序!")
main()
while True:
    print("是否继续计算?继续请输入y,输入其他内容将结束程序。")
    if input() == "y":
        main()
    else:
        print("感谢使用工作量计算小程序!")
        break


# 图书管理系统

import time

from pymysql import Connection


class Sql:
    conn = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="123456",
        autocommit="True"
    )

    def ReturnCursor(self, conn):
# ‘itcast’是我的数据库名称,这里要改成自己的数据库
        conn.select_db("itcast")
        return conn.cursor()

# Book类其实我没用到
class Book:
    def __init__(self, name, author, state=1):
        self.name = name
        self.author = author
        self.state = state

    def showInfo(self):
        sql = Sql()
        cursor = sql.ReturnCursor(sql.conn)
        cursor.execute("select * from Book")
        results = cursor.fetchall()
        for result in results:
            print(result)


class BookManage(Sql, Book):
    info = [("查询全部书籍", "showBooks"), ("添加书籍", "addBook"),
            ("借阅书籍", "lendBook"), ("归还书籍", "returnBook"), ("退出系统", "exit")]

    def __init__(self):
        self.books = []

    def showBooks(self):
        cursor = self.ReturnCursor(self.conn)
        cursor.execute("select * from Book")
        results = cursor.fetchall()
        print("图书馆当前有以下书籍:")
        for result in results:
            # result为元组
            print(result)

    def addBook(self):
        book = Book(input("请输入您想添加的书籍名称:"), input("请输入书籍的作者:"))
        try:
            if not self.checkBook(book.name):
                cursor = self.ReturnCursor(self.conn)
                cursor.execute(f"insert Book values('{book.name}','{book.author}','{book.state}')")
                print("添加书籍成功!")
        except:
            print("书籍已经存在不能添加!")

    def checkBook(self, name):
        cursor = self.ReturnCursor(self.conn)
        cursor.execute(f"select name,author from Book where name = '{name}'")
        result = cursor.fetchone()
        if result is not None:
            return True
        else:
            return False

    def lendBook(self):
        print("请输入您想借阅的书籍名称:")
        name = input()
        if self.checkBook(name):
            cursor = self.ReturnCursor(self.conn)
            cursor.execute(f"update Book set state='0' where name = '{name}'")
            print("借书成功!")
        else:
            print("抱歉,图书馆中没有该书哦!")

    def returnBook(self):
        print("请输入您想归还的书籍名称:")
        name = input()
        if self.checkBook(name) and self.check_state(name):
            cursor = self.ReturnCursor(self.conn)
            cursor.execute(f"update Book set state='1' where name = '{name}'")
            print("还书成功!")
        else:
            print("抱歉,图书馆中没有该书哦!")

    def check_state(self, name):
        cursor = self.ReturnCursor(self.conn)
        cursor.execute(f"select state,name from Book where name = '{name}'")
        result = cursor.fetchone()
        if '0' == result[0]:
            return True
        else:
            return False

    def exit(self):
        print("感谢您的使用,系统即将退出")
        time.sleep(2)
        self.conn.close()


def main():
    Book_Manager = BookManage()
    menu = Book_Manager.info
    while True:
        for i in range(len(menu)):
            print(i + 1, menu[i][0])
        choose = eval(input("输入序号进行操作:"))
        getattr(Book_Manager, menu[choose - 1][1])()
        if choose == 5:
            break


main()
1 、工作量计算器
已知对于标准大小(值为 1 )的项目,需要 1 个人用 80 个工时完成。请设计一个工作量
计算器,实现两个以下功能:
1 )已知项目大小、工作人数,计算出每个人需要多少工时才能完成项目;
2 )已知项目大小、总工时,计算要总工时内完成项目,至少需要的工作人数。
要求具备人机接口,提示用户输入信息,并根据信息进行相关计算,程序运行结果如下
图所示。
2 、图书管理系统
1 )设计一个图书管理系统。系统具有以下几个功能:
查询全部书籍、添加书籍、借阅书籍、归还书籍和退出系统。
请注意处理可能出现的异常 ,比如(不限于以下异常):
n 加入图书时,系统中已有该图书;
n 借阅图书时,系统中没有该图书;
n 归还图书时,系统中没有该图书
n ……
定义如下所示书籍管理类书籍类 Book 和书籍管理类 BookManage (提示信息中 pass 代表需
要补充代码):
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_73931224

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值