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
购物车优化作业
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('编号输入错误')
014--python运算符和作业改进的更多相关文章
- python运算符重载
python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...
- Python运算符,python入门到精通[五]
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...
- Python运算符与表达式
Python运算符包括赋值运算符.算术运算符.关系运算符.逻辑运算符.位运算符.成员运算符和身份运算符. 表达式是将不同类型的数据(常亮.变量.函数)用运算符按照一定得规则连接起来的式子. 算术运算符 ...
- (三)Python运算符
一.python运算符相关 Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 1.python算数运算符 ...
- 【Python 补充01】Python运算符
Python运算符 举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. 1.算术运算符 + - * / # 加减乘除 % # 取模(返回除 ...
- python运算符与数据类型
python运算符 Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 以下假设变量: a=10,b=20: ...
- Python——运算符
Python算术运算符 以下假设变量: a=10,b=20: 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 ...
- Python学习day5作业
目录 Python学习day5作业 ATM和购物商城 1. 程序说明 2. 基本流程图 3. 程序测试帐号 4. 程序结构: 5. 程序测试 title: Python学习day5作业 tags: p ...
- 基于python复制蓝鲸作业平台
前言 去年看武sir代码发布的视频无意中听到了蓝鲸平台但是一直没深究,前一段时间公司要搞一个代码发布平台,但是需求变化很多一直找不到一个很好的参考 模板,直到试用了一下蓝鲸作业平台发现“一切皆作业”的 ...
随机推荐
- Cocoa开发中, 如何用全局变量
比如在tabbar的开发中,可以某个页面的数据需要在back到此页面后依然有效. 可以用 appDelegate 这个对象来创建你的全局数据 这个内建的对象,在 APP 启动时产生,在 APP 退出时 ...
- Tar压缩文件
[root@test /root]# tar [-zxcvfpP] filename [root@test /root]# tar -N 'yyyy/mm/dd' /path -zcvf targ ...
- influxDB的安装和简单使用
单机的influxdb免费,集群的是收费的 1.安装,参考官方文档 https://portal.influxdata.com/downloads RedHat & CentOS: wget ...
- 【Todo】Java类型转换总结
参考 http://www.cnblogs.com/lwbqqyumidi/p/3700164.html 这篇文章也可以对照着看:http://www.360doc.com/content/10/09 ...
- oracle下session的查询与删除
oracle下session的查询与删除 1.查询当前session SQL> select username,sid,serial# from v$session where username ...
- FLEX接收外部参数 .
FLEX参数传递与FLASH有点不同 login..swf?name=aa&password=bb Flex上是这样接收参数的 myname=mx.core.Application.appli ...
- 漫谈程序猿系列:无BUG不生活
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Chrome浏览器 js 关闭窗口失效解决方法
//获取元素ID var DelHtml = document.getElementById("imgdel"); //alert(DelHtml); //添加点击事件 DelHt ...
- Lazy freeing of keys 对数据的额异步 同步操作 Redis 4.0 微信小程序
https://github.com/antirez/redis/blob/4.0-rc1/00-RELEASENOTES 数据缓存 · 小程序 https://developers.weixin.q ...
- acd - 1427 - Nice Sequence(线段树)
题意:一个由n个数组成的序列(序列元素的范围是[0, n]).求最长前缀 j .使得在这个前缀 j 中对于随意的数 i1 < i2.都满足随意的 m <= j.i1 在前 m 个数里出现的 ...