014--python运算符和作业改进
2017-04-16 16:44
211 查看
一、运算符
% 取模,返回商的余数
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版)
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
相关文章推荐
- Python3-小甲鱼教程 【课后作业及答案】004改进我们的小游戏
- 作业1+2.四则运算(改进后完整版,用python写的)_064121陶源
- Python3-小甲鱼教程 【课后作业及答案】004改进我们的小游戏
- python运算符及其用法
- python中的in运算符
- 【笔记】Excel技巧—014 多窗口协同作业
- 用Python实现一个细粒度hadoop作业监控分析工具
- python中的in运算符
- Python命令行选项/运算符
- python 教程 第三章、 运算符与表达式
- (二)python的数据类型与运算符
- Python学习(三) -- 运算符与表达式
- Python基础知识(三)--基本的异常处理、算术运算符、输入/输出
- python 教程 第十四章、 地址薄作业
- Python 的运算符
- python 运算符 供重载参考
- 从C#到Python —— 2 运算符、表达式和流程控制
- Lemon的Python学习笔记-数值和运算符
- C#作业——表达式计算器+小数+运算符优先级