day_work_02

day_work_02

------Python是一个优雅的大姐姐

作业一

 

设计思路(四个if一个while)

  1. 首先我先把商品用列表加元组的形式保存,然后将商品遍历出来。
  2. 键盘输入薪水。
  3. (if)判断输入薪水数据类型是否为数字:是!继续运行;否!打印“输入薪水不为数字!”退出程序。
  4. (while)循环开始
  5. (if)输入商品编号判断是否为数字:是!进行下一步;否则判断是否为q:是!打印购物车商品和余额,退出程序;否!打印“输入错误!”回到第四步。
  6. (if)判断编号是否可以找到对应商品:是!则进行下一步;否!则提示“不存在该商品编号!”回到第四步。
  7. (if)判断余额是否足够购买商品:是!将商品加入购物车,余额减少;否!打印“余额不足!”,返回第四步。

运行代码

# Author:Xiong

product_list=[
    ('iphone6s',5800),
    ('macbook',9000),
    ('coffee',30),
    ('python book',80),
    ('bicyle',1500),

]
shopping_car = []

salary = input('请输入您的薪水:')

for i,v in enumerate(product_list,1):
    print(i,v[0],v[1])

if salary.isdigit():
    save = int(salary)
    while True:
        ch = input('输入您需要购买的商品编号[q退出]')
        if ch.isdigit():
            ch = int(ch)
            if ch>0 and ch <= len(product_list):
                if product_list[ch-1][1] > save:
                    print('余额不足,',save - product_list[ch-1][1])
                else:
                    save -= product_list[ch-1][1]
                    print('购买%s成功!当前余额%d'%(product_list[ch-1],save))
                    shopping_car.append(product_list[ch-1])
            else:
                print('不存在该商品编号!')
        elif ch == 'q':
            print('购买商品为%s' % shopping_car)
            print('余额为%d' % save)
            print('欢迎下次光临!')
            break
        else
:
            print('输入错误!')
else:
   print('输入薪水不为数字!')

运行结果

 

作业二

 

设计思路

  1. 创建一个多级字典,输入相应数据。
  2. 设置三个变量,

exit_flag = False:终结循环的标志,本程序未设置退出程序操作。

current_layer = menu:

layers = [menu]:

  1. (while)进入while循环(未设置跳出循环操作)
  2. 遍历并输出字典current_layer中的key值
  3. 键盘输入key值,或者b操作
  4. (if)判断键盘输入的choice是否为‘b’:是!进行返回上一级操作;否!判断是否是字典current_layer中的key值:否!返回本层字典的输入界面;是!进入下一层字典。

运行代码

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
}


exit_flag = False

current_layer = menu

layers = [menu]

while not  exit_flag:
    for k in current_layer:
        print(k)
    choice = input(">>(输入b返回上一级):").strip()
    if choice == "b":
        current_layer = layers[-1]
        layers.pop()
    elif choice not  in current_layer:continue
    else
:
        layers.append(current_layer)
        current_layer = current_layer[choice]

运行结果

 

原文地址:https://www.cnblogs.com/xzmxddx/p/8365413.html