014--python运算符和作业改进

一、运算符

    % 取模,返回商的余数

    10/3     3.33333333335

    10//3    3   地板除,取整数,不是四舍五入

    a = 3  b = 5  ----->   a<b and a==4 or b<10 and a>1

    or前面的条件成立,则不走or后面的式子;or前面的条件不成立,则不考虑前面的式子,直接运算后面的

    '123'.isdigit() 判断一个字符串是否可以转换为数字

    身份判断:type(123) is  int    type('123') is  str

    位运算:

              128  64  32  16  8  4  2  1

   a=60       0    0  1   1    1  1  0  0

   b=13   0    0   0  0    1   1  0  1

 &按位与      0    0   0  0    1   1  0  0

 |按位或   0     0   1  1    1   1  0  1

二、作业

    三级菜单(low版)

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}
break_flag = True
while break_flag:
    for i in menu:
        print(i)
    choice1 = input('>:').strip()
    if len(choice1)==0:continue
    if choice1=='q':
        break_flag = False
        continue
    if choice1 in menu:
        while break_flag:
            for j in menu[choice1]:
                print(j)
            choice2 = input('>>:').strip()
            if len(choice2)==0:continue
            if choice2=='b':break
            if choice2=='q':
                break_flag = False
                continue
            if choice2 in menu[choice1]:
                while break_flag:
                    for k in menu[choice1][choice2]:
                        print(k)
                    choice3 = input('>>>:').strip()
                    if len(choice3)==0:continue
                    if choice3=='b':break
                    if choice3=='q':
                        break_flag = False
                        continue
                    if choice3 in menu[choice1][choice2]:
                        while break_flag:
                            for m in menu[choice1][choice2][choice3]:
                                print(m)
                            choice4 = input('>>>>:')
                            if len(choice4)==0:continue
                            if choice4=='b':break
                            if choice4=='q':
                                break_flag = False
                                continue
View Code

    购物车优化作业

shopping_cart = {} #购物车
product_list = [
                ['iphone',5800],
                ['小米手机',2000],
                ['茅台酒',650],
                ['',5],
                ['电饭锅',200]
]
salary = int(input('Input your salary:'))
while True:
    index = 0
    for product in product_list:
        print(index,product)
        index+=1
    choice = input('请输入商品编号:')
    if choice.isdigit():
        choice = int(choice)
        if choice>=0 and choice<=len(product_list):
            product = product_list[choice]
            if product[1] <= salary:
                if product[0] in shopping_cart:
                    shopping_cart[product[1]]+=1
                else:
                    shopping_cart[product[0]]=[product[1],1]
                salary-=product[1]
                print('购物车已添加:'+product[0]+' ,您的余额为:'+str(salary))
            else:
                print("工资不够,商品的价格为:%s ,还差:%s"%(str(product[1]),product[1]-salary))
        else:
            print('编号不存在,请重新输入')
    elif choice=='q':
        print('-----------您已购买以下商品-----------')
        id_count = 1
        total_cost = 0
        print('id       商品      单价      数量      总价')
        for key in shopping_cart:
            print("%s\t\t%s\t\t%s\t\t%s\t\t%s"
                  %(id_count,
                    key,
                    shopping_cart[key][0],
                    shopping_cart[key][1],
                    shopping_cart[key][0]*shopping_cart[key][1]
                    )
                  )
            id_count+=1
            total_cost+=shopping_cart[key][0]*shopping_cart[key][1]
        print('您的总花费为:%s'%total_cost)
        print('您的余额为:%s'%salary)
        print('----------------end---------------')
    else:
        print('编号输入错误')
View Code

 

  

    

转载于:https://www.cnblogs.com/guotianbao/p/6718349.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值