复习python字典,类属性,类方法,实例对象时的练习
设计一个数据信息管理系统。通过使用字典存储用户信息和书籍信息,使用类属性和方法来注册用户。中间涉及到类的方法,字典的嵌套以及字典的方法的使用。整个系统较为简单,没有设计复杂操作,作为复习时的小练习。
代码如下:
# 书籍信息管理系统
# 初始用户信息
total = {
202001:{"name":"小红","password":"123456","email":"112112112@qq.com"},
202002:{"name":"小明","password":"111222","email":"123975632@qq.com"},
202003:{"name":"小美","password":"753951","email":"468446448@qq.com"}
}
# 初始书籍信息
book = {
"杂志":{"01":{"name":"青年文摘","price":13},"02":{"name":"看天下","price":15}},
"小说":{"03":{"name":"老人与海","price":25},"04":{"name":"基督山伯爵","price":50}}
}
# account记录自动给新注册用户的编号
account = 202003
class user(object):
def __init__(self,account,name,password,email):
self.name = name
self.password = password
self.email = email
self.account = account
total.update({account:{"name":self.name,"password":self.password,"email":self.email}})
# 进入系统
while 1:
judge_2 = input("Do you have an account? yes/no :")
if judge_2=="yes":
print("Please log in. And you have three chance to login.")
count = 3
while count > 0:
name_judge = input("input your name:")
password_judge = input("input your password:")
judge_1 = False
for i in range(len(total)):
if total.get(i + 202001).get("name") == name_judge and total.get(i + 202001).get(
"password") == password_judge:
print("-----------------")
print("Login successful")
print("-----------------")
judge_1 = True
break
if judge_1 ==True:
break
else:
count = count - 1
print("input error! please try again. And you only have ", count, " chance")
if judge_1 == False:
print("You have used up three chances. Please try again later")
while 1:
print("please choose the number for what do you want to do?")
print("1:View books by type")
print("2:View all book information")
print("3:Update book information")
print("4:Delete the books")
print("5:Add book information")
print("6:input 6 to exit")
choose = int(input())
if choose == 1:
book_type = input("please input the type of books:")
for key in book.get(book_type):
print(key, ":", book.get(book_type)[key])
elif choose == 2:
for key in book:
print(key, ":", book[key])
elif choose == 3:
book_name = input("Enter the name of the book you want to change :")
book_newname = input("input the new name of this book:")
book_newprice = input("input the new price if this book:")
judge_3 = False
for key1 in book:
for key2 in book.get(key1):
if book.get(key1).get(key2).get("name") == book_name:
book.get(key1).get(key2).update({"name": book_newname})
book.get(key1).get(key2).update({"price": book_newprice})
judge_3 = True
print("successfully")
break
if judge_3 == True:
break
if judge_3 == False:
print("no such a book")
elif choose == 4:
book_number = input("please input the book number which you want to delete:")
judge_4 = False
for key3 in book:
for key4 in book.get(key3):
if key4 == book_number:
book.get(key3).pop(key4)
judge_4 = True
print("successfully")
break
if judge_4 == True:
break
if judge_4 == False:
print("no such a book which number is ", book_number)
elif choose == 5:
book_type = input("input the new book type:")
book_number = input("input the new book number:")
book_name = input("input the new book name:")
book_price = input("input the new book price")
book.update({book_type: {book_number: {"name": book_name, "price": book_price}}})
print("successfully")
elif choose == 6:
break
else:
print("input error!! please try again!")
print()
elif judge_2=="no":
print("Please register first")
name = input("input your name:")
password = input("input your password:")
email = input("input your email:")
account = account + 1
people = user(account, name, password, email)
print("Registered successfully !")
else:
print("error!!please input again!")
以上