第二次作业

1.打印九九乘法表

for i in range(1,10):
for k in range(1,10-i):
print(end=" ")#此处为8个字节
for j in range(1,i+1):
print(f"{j}×{i} = {i * j}", end=" ")#此处为8个字节
print(" ")

2.打印金字塔

def pyramid(n):
for i in range(1, n):
print(' ' * (n - (i - 1)) + '*' * (2 * i - 1))
pyramid(10)

3.打印三级菜单

menu = {
'河北省':{
'沧州市':{
'任丘市':{
'石门桥镇':{},
'长丰镇':{}
},
'泊头市':{
'泊镇':{},
'富镇':{}
}
},
'石家庄市':{
'赵县':{
'赵州镇':{},
'韩村镇':{}
},
'正定县':{
'正定镇':{},
'新安镇':{}
}
},
'唐山市':{
'遵化市':{
'遵化镇':{},
'马兰峪镇':{}
},
'迁安市':{
'蔡元镇':{},
'上庄乡':{}
}
}
},
'北京市':{
'朝阳区':{
'三里屯':{
'南楼':{},
'北楼':{}
},
'香河':{
'西坝河':{},
'光熙门':{}
}
},
'东城区':{
'东华门':{
'东厂':{},
'智德':{}
},
'建国门':{
'赵家楼':{},
'大雅宝':{}
}
},
'西城区':{
'新街口':{
'北顺':{},
'中直':{}
},
'金融街':{
'文昌':{},
'手帕':{}
}
}
}

}
current_layer,layers = (menu,[])
while True:
for keys in current_layer:print(keys)
choice = input('>>>:').strip() #删除多余字符
if not choice:continue
if choice in current_layer:
layers.append(current_layer);current_layer = current_layer[choice]
elif choice == 'r': #r代表返回上一级菜单
if len(layers)!= 0:
current_layer = layers.pop()
else:
print('回到上一层')
elif choice == 's': #s代表退出程序
exit('退出程序')

4.购物车程序

products = [
{"name":'iphone',"price":4888},
{"name":'三星',"price":5888},
{"name":'华为',"price":8888},
{"name":'小米',"price":6888},
]
wages = int(input('请输入您的工资:'))
shopping_car =[] #购物车
exit_flag = False #控制While循环
while not exit_flag:
print('*****商品列表*****')
for index,i in enumerate(products):
print(index,i) #打印商品列表
choice = (input('请输入商品编码:'))
if choice.isdigit():
choice = int(choice) #字符串转为数值
if 0 <= choice < len(products):
if wages >= products[choice].get('price'):
wages -= products[choice].get('price')
print('您的余额为:',wages)
shopping_car.append(products[choice])
print('已将您要购买的产品:%s 添加至您的购物车'%(products[choice]))
else:
print('您的余额不足!')
elif choice == 'q':
print('*****您已购买的产品*****')
for index, k in enumerate(shopping_car):
print(index,k)
print('账户余额为:',wages)
exit_flag = True #循环结束


原文地址:https://www.cnblogs.com/datatool/p/13341999.html