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
代表需
要补充代码):