用户管理系统
surface = """ #三引号是Python的注释符号,但也可以作为字符串输出
****************************************
用户管理系统
****************************************
1、注册新用户
2、用户登录
3、用户注销
4、用户信息显示
5、退出系统
"""
userinfo = { # 用来保存数据的字典
'root': {
'name': 'root',
'password': 'redhat',
'gender': 1,
'email': '',
'age': '12'
},
}
gender_choice = [0, 1, 2]
def CreateUser(): # 创建用户
print("注册用户界面".center(50, "*"))
name = input("*注册用户名:")
if name in userinfo:
print("用户已存在,请更换注册名")
else:
password = input("*用户密码:")
while True:
gender1 = input('*性别(0-男,1-女,2-其他)')
gender = int(gender1)
if gender in gender_choice:
break
else:
print("请输入正确的选择")
email = input("用户邮箱:")
if not email:
email = None
age = input('年龄:')
if not age:
age = None
else:
age = int(age)
userinfo[name] = {
'name': name,
'password': password,
'gender': gender,
'email': email,
'age': age,
}
print("%s 用户注册成功!!!" % (name))
def UserLogin(): # 用户登录
print("用户登录界面".center(50, '*'))
trycount = 0
while trycount < 3: # 用户输入错误三次则结束循环
name = input("登录用户名:")
if name not in userinfo:
print("用户未注册")
break
password = input("登录密码:")
trycount += 1
if password == userinfo[name]['password']:
print("恭喜%s登录成功" % (name))
break
else:
print("请输入正确的用户名或密码!")
else:
print("已登录三次,请稍后再试")
def DeleteUser(): # 删除用户
print("用户注销界面".center(50, '*')) # 表示50的长度,字符串放中间,空白的地方自动填充*号
name = input("注销用户名:")
if name not in userinfo:
print("用户未注册")
else:
password = input("登录密码:")
if password == userinfo[name]['password']:
userinfo.pop(name)
print('恭喜注销%s成功' % (name))
def UserInfo():
for key, value in userinfo.items(): # 迭代字典
print("用户:%s" % (key)),
print("性别:%d" % (value['gender'])),
print("邮箱:%s" % (value['email'])),
print("年龄:%s" % (value['age'])),
print("\n\n")
def main(): # 主函数
while True:
print(surface)
choice = input("Choice:").strip() # 删除前后空白字符串
if choice == '1':
CreateUser()
elif choice == '2':
UserLogin()
elif choice == '3':
DeleteUser()
elif choice == '4':
UserInfo()
elif choice == '5':
exit() # 关闭程序
else:
print("请输入正确的选择")
main()
宠物管理系统
PETS = [] #全局变量
def add_pet():
ID = input("请输入宠物编号:")
name = input("请输入宠物名称:")
category = input("请输入宠物种类:")
price = input("请输入宠物价格:")
pet = {'id':ID,'name':name,'category':category,'price':price}
PETS.append(pet)
print("恭喜宠物添加成功!")
def search_pet():
name = input("请输入宠物名称:")
for pet in PETS:
if pet['name'] ==name:
text = "编号:{},名称:{},种类:{},价格:{}".format(
pet['id'],
pet['name'],
pet["category"],
pet['price']
)
print(text)
def delete_pet():
ID = input("请输入宠物编号:")
for pet in PETS:
if pet['id'] == ID:
PETS.remove(pet)
print("删除宠物成功!")
break
def list_pet():
for pet in PETS:
text = "编号:{},名称:{},种类:{},价格:{}".format(
pet['id'],
pet['name'],
pet["category"],
pet['price']
)
print(text)
def main():
print('='*30)
print('1.添加宠物')
print('2.查找宠物')
print('3.删除宠物')
print('4.列出宠物')
print('5.退出宠物')
print('='*30)
while True:
option = input("请输入选项:")
if option == '1':
add_pet()
elif option == '2':
search_pet()
elif option == '3':
delete_pet()
elif option == '4':
list_pet()
elif option == '5':
break
else:
print("请输入正确的选项")
main()