"""
作业一:for循环嵌套
https://www.cnblogs.com/linhaifeng/articles/7133167.html#_label14
作业二: 三级菜单
要求:
打印省、市、县三级菜单
可返回上一级
可随时退出程序
作业三:请闭眼写出购物车程序
需求:
用户名和密码存放于文件中,格式为:egon | egon123
启动程序后,先登录,登录成功则让用户输入工资, 然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
"""
作业一(九九乘法表):
for i in range(1,10):
for j in range(1,i+1):
print('%s*%s=%s'%(i,j,i*j),end=' ')
print()
作业一(金字塔):
max_level=5
for current_level in range(1,max_level+1):
for i in range(max_level-current_level):
print(' ',end='')
for j in range(2*current_level-1):
print('*',end='')
print('')
作业二(三级菜单):
dic = {
'四川省': {
'广元市': {
"剑阁县": ["剑门关", "123", 321],
"公兴镇": ["九龙村", 222, 333]
},
'成都市': {
"青羊区": ["新华公园", "456", 321],
"金牛区": ["成都火车站", 666, 888]
},
'绵阳市': {
"游仙区": ["芙蓉酒店", "789", 654],
"涪城区": ["百盛购物", 777, 999]
},
},
'广东省': {
'中山市': {
"红木家具": ["青竹路", "123", 321],
"安堂村": ["九龙门", 222, 333]
},
'珠海市': {
"锦江乐园": ["珠海大道", "456", 321],
"海上乐园": ["人民大道", 666, 888]
},
'东莞市': {
"购物广场": ["东莞大道", "789", 654],
"公共厕所": ["哪里都路", 777, 999]
},
},
'上海': {
'宝山区': {
"万达广场": ["共和新路", "657", 987],
"家乐福": ["共江路", 111, 444]
},
'黄浦区': {
"苹果专卖": ["南京路", "456", 321],
"人民广场": ["南京西路", 666, 888]
},
'青浦区': {
"老男孩": ["华徐公路", "789", 654],
"华容道": ["黄江路", 777, 999]
},
}
}
版本一:功能实现
end_flag = False
while not end_flag:
for menu_one in dic:
print(menu_one)
choice_one = input("输入省名进入市(输入q退出程序):").strip()
if choice_one in dic:
while not end_flag:
for menu_two in dic[choice_one]:
print(menu_two)
choice_two = input("输入市名进入县(输入n返回上一层或输入q退出程序):").strip()
if choice_two in dic[choice_one]:
while not end_flag:
for menu_three in dic[choice_one][choice_two]:
print(menu_three)
choice_three = input("输入县名进入街道(输入n返回上一层或输入q退出程序):").strip()
if choice_three in dic[choice_one][choice_two]:
while not end_flag:
for menu_four in dic[choice_one][choice_two][choice_three]:
print(menu_four)
choice_four = input("最后一层,输入n返回或输入q退出程序:").strip()
if choice_four == 'n':
break
elif choice_four == 'q':
end_flag = True
else:
print("****请输入正确的指令****")
elif choice_three == 'n':
break
elif choice_three == 'q':
end_flag = True
else:
print("****请输入正确的县名或正确的指令*****")
elif choice_two == 'n':
break
elif choice_two == 'q':
end_flag = True
else:
print("****请输入正确的市名或正确的指令*****")
elif choice_one == 'q':
end_flag = True
else:
print("****请输入正确的省名或正确的指令*****")
版本二功能实现
end_flag = False
def menu_choice(kwargs):
"""
1、先遍历字典的key
2、根据输入的key进入嵌套的字典
3、再调用自己取出嵌套的key
"""
global end_flag # 这里定义了一个局部变量使用global来修改全局变量
while not end_flag:
for menu_one in kwargs:
print(menu_one)
choice = input("请选择进入, 输入n返回上一层,q退出程序:").strip()
if choice in kwargs:
# print(kwargs[choice])
menu_choice(kwargs[choice]) # 调用自己,并取出参数kwargs中key走到for循环打印
elif choice == 'n':
break
elif choice == 'q':
end_flag = True
else:
print("****请输入正确的名称或正确的指令****")
menu_choice(dic) # 调用函数menu_choice并将dic这个字典传给kwargs进入函数体内部处理
作业三:购物车程序
product_list = [
("黑色毛衣", 68),
("iphone", 8849),
("python书籍", 99),
("蓝色牛仔裤", 199),
("ThinkPad", 12800),
("linux书籍", 89),
]
hopping_list = [] # 定义空列表存放用户购买的商品
login_count = 0
while login_count < 3:
username = input("请输入您的用户名: ").strip()
password = input("请输入您的密码: ").strip()
with open('user.txt', 'r') as f:
user_list = f.readlines()
for user_line in user_list:
user, passwd = user_line.strip('\n').replace(" ", "").split('|')
if user == username and password == passwd:
print("恭喜,登陆成功")
while True:
user_salary = input("请输入您的工资:").strip()
if user_salary.isdigit():
user_salary = int(user_salary)
while True:
for item in product_list:
print(product_list.index(item),item) # 打印商品列表索引
choice_product = input("请输入数字来选择您需要购买的商品或输入q退出>>>:")
if choice_product.isdigit():
choice_product = int(choice_product)
# 判断用户输入的索引是否小于商品列表索引并且大于商品索引开始值
if len(product_list) > choice_product >= 0:
# 如果满足通过下标取出商品
product_item = product_list[choice_product]
if product_item[1] <= user_salary:
# 将用户购买的商品存在一个空列表
shopping_list.append(product_item)
# 扣除用户工资
user_salary -= product_item[1]
print("已将商品 %s 添加至购物车,您的余额为 %s 元" %(product_item,user_salary))
else:
print("您的余额不足,无法进行购买,您的余额为 %s 元" %user_salary)
else:
print("您输入的 %s 号商品不存在,请选择列表中的商品" % choice_product)
elif choice_product == 'q':
if len(shopping_list):
print("=======您已购买的商品=========")
for view_product_list in shopping_list:
print(view_product_list)
print("您的剩余的余额为: %s 元,欢迎下次光临" %user_salary)
exit()
else:
exit_input = input("您未购买任何产品,确认退出吗? 输入q退出:")
if exit_input == 'q':
exit()
else:
print("请输入正确指令")
else:
print("您输入的工资非数字,请重新输入")
else:
if login_count != 2:
print('用户或密码输入错误,请重新输入, 还有 %s 次机会' %(2-login_count))
login_count += 1