2-16练习

作业1

写程序:购物车

需求:

启动程序后,让用户输入工资,然后打印商品列表:

允许用户根据商品编号购买商品

用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒。

用户可以一直购买商品,也可随时退出,退出时,打印已购买商品的余额

 

#判断输入的工资是否为数字:
while True:
    salary = input("your salary:")
    if salary.isdigit():
        salary = int(salary)
        break
    else:
        print("input your salary again:")

#商品列表
ProductList = [("Iphone", 5800),
    ("Mac Pro", 12000),
    ("Bike", 800),
    ("Starbuck Latte", 30),
    ("Alex Python", 80),
    ("Sun glasses", 200)
]

#用户选到购物车中的商品列表
ChooseList = []

while True:
    #打印商品列表,利用map函数,打印出的元祖不带括号
    for index,item in enumerate(ProductList):
        print(index,end =' ')
        print(' '.join(map(str,item)))
    choose =  input("input the index of product you want or input 'q' to quit")
    
    #如输入为'q'退出
    if choose == 'q':   
        break
    #判断输入的是否为数字
    elif choose.isdigit():
         choose = int(choose)
         #判断选择的商品编号是否存在
         if choose >=0 or choose < len(ProductList):
            if ProductList[choose][1] < salary:
                ChooseList.append(ProductList[choose])
                salary = salary - ProductList[choose][1]
            else:
                print("sorry! You can't afford!")
                continue
         else:
            print("invalid input ,please input again")
    else:
        print("invalid input ,please input again")
        continue
    print('Products you choose:')

#打印用户选择的商品及余额
for index, item in enumerate(ChooseList):
    print(index,item)
print('banlance:',salary)   

 

 作业2

写程序:多级菜单

需求:

现有省、市、县3级结构,要求程序启动后,允许用户可依次选择进入各子菜单

可以任意一级菜单返回上一级

可以在任意一级菜单退出程序

所需知识点:列表、字典

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
                      },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{}
                     }  
                },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{}
                   },
            '天通苑':{},
            '回龙观':{}
                }, 
        '朝阳':{},
        '东城':{}
            },
    '上海':{
        '闵行':{
            '人民广场':{
                    '炸鸡店':{}
                        }
               },
        '闸北':{
            '火车站':{
                '携程':{}
                      }
               },
        '浦东':{}      
            },
    '山东':{}
        }
 #用flag来标识当前菜单层数
flag = 0

#显示第一级菜单
for key in menu.keys():
        print(key)
        
#用来存放用户的选择
ChooseList = []


while True:
  
    choose = input("请选择菜单,按q返回上一级")
    if choose == 'q':
        flag -= 1
        if   flag == 2:
                for key in menu[ChooseList[0]][ChooseList[1]].keys():
                     print(key)
                continue
        elif flag == 1:
                for key in menu[ChooseList[0]].keys():
                     print(key)
                continue
        elif flag ==0:
                for key in menu.keys():
                    print(key)
                continue
        else:
            break
    else:
        if flag == 0:
            if menu.get(choose) != None:
                ChooseList.append(choose)
                for key in menu.get(choose):
                    print(key)
                flag += 1
            else:
                print("无效输入")
        elif flag ==1 :
            if menu[ChooseList[0]].get(choose) != None:
                ChooseList.append(choose)
                for key in menu[ChooseList[0]].get(choose):
                    print(key)
                flag +=1
            else:
                print("无效输入")
        elif flag == 2:
            if menu[ChooseList[0]][ChooseList[1]].get(choose) != None:
                ChooseList.append(choose)
                for key in menu[ChooseList[0]][ChooseList[1]].get(choose):
                    print(key)
                flag +=1
            else:
                print("无效输入")

 

posted @ 2019-07-24 16:28  echo少儿编程  阅读(179)  评论(0编辑  收藏  举报