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代码发布的视频无意中听到了蓝鲸平台但是一直没深究,前一段时间公司要搞一个代码发布平台,但是需求变化很多一直找不到一个很好的参考 模板,直到试用了一下蓝鲸作业平台发现“一切皆作业”的 ...
随机推荐
- android 图片的平移,缩放和旋转
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Go语言并发之美
简介 多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...
- Go -- log4go日志
折腾: [已解决]go语言中实现log信息同时输出到文件和控制台(命令行) 期间,已经通过io的MultiWriter搞定了同时输出信息到文件和console,但是不支持level. 所以,再去试试这 ...
- 高速清除winXP系统中explorer.exe病毒
关于这个explorer.exe病毒.是眼下xp最为常见的一个病毒,会大量的消耗系统资源,造成电脑特别的卡顿. 1.关闭还原(假设没有,则跳过),为的是防止我们改动后,还原之后又回来了. 2.打开注冊 ...
- C#模拟登录Twitter 发送私信、艾特用户、回复评论
这次做成了MVC程序的接口 private static string UserName = "用户名"; private static string PassWord = &qu ...
- ActiveMQ(三) 转
package pfs.y2017.m11.mq.activemq.demo03; import javax.jms.Connection; import javax.jms.ConnectionFa ...
- 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划
淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划 SQL编译解析三部曲分为:构建语法树,制定逻辑计划,生成物理执行计划.前两个步骤请参见我的博客<<淘宝数据库O ...
- 解决编译twrp3.0.3遇到的问题
1. 问题: ninja: error: '/home/jessie/OMNI/out/target/product/m1/obj/SHARED_LIBRARIES/libcryptfs_hw_int ...
- 解压Zip
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import ja ...
- java验证码图片
package com.su.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; impor ...