day_work_02
day_work_02
------Python是一个优雅的大姐姐
作业一

设计思路(四个if一个while)
- 首先我先把商品用列表加元组的形式保存,然后将商品遍历出来。
- 键盘输入薪水。
- (if)判断输入薪水数据类型是否为数字:是!继续运行;否!打印“输入薪水不为数字!”退出程序。
- (while)循环开始
- (if)输入商品编号判断是否为数字:是!进行下一步;否则判断是否为q:是!打印购物车商品和余额,退出程序;否!打印“输入错误!”回到第四步。
- (if)判断编号是否可以找到对应商品:是!则进行下一步;否!则提示“不存在该商品编号!”回到第四步。
- (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('输入薪水不为数字!')
运行结果

作业二

设计思路
- 创建一个多级字典,输入相应数据。
- 设置三个变量,
exit_flag = False:终结循环的标志,本程序未设置退出程序操作。
current_layer = menu:
layers = [menu]:
- (while)进入while循环(未设置跳出循环操作)
- 遍历并输出字典current_layer中的key值
- 键盘输入key值,或者b操作
- (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]
运行结果

day_work_02的更多相关文章
随机推荐
- hashmap C++实现
hashmap.h #ifndef _HASHMAP_H_ #define _HASHMAP_H_ template<class Key, class Value> class HashN ...
- aws和ufile挂载数据盘EBS
aws的话挂载的ebs需要格式化,参考:https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/ebs-using-volumes.html ...
- mysql拆分逗号一列变多行
需求: SELECT ), ) FROM TABLE a INNER JOIN mysql.help_topic b )
- [转帖]Windows 下如何配置Oracle ASM???
Windows 下如何配置Oracle ASM??? candon123关注10人评论16725人阅读2011-02-09 21:40:57 本篇介绍了如何在windows下创建裸设备,并创建AS ...
- HDU 4253-Two Famous Companies(二分+最小生成树)
Description In China, there are two companies offering the Internet service for the people from all ...
- thinkphp6下无法获取header头中的Authorization(apache版)
今天遇到在thinkphp框架中获取不到header头里边的 Authorization ,后来在.htaccess里面加多一项解决,记录下: <IfModule mod_rewrite.c&g ...
- QQ登錄實現功能
<html> <head> <title></title> </head> <body> <form> <p& ...
- 关于tomcat部署项目的问题
问题是这样的 之前用tomcat8.5部署的项目,结果启动项目一直三个端口被占用,浏览器也打不开目标网页 卸了8,装了9.先配置的一大堆,结果可以打开Tomcat的主页locahost:8080,到此 ...
- git简易操作手册
从远程仓库新建 $ git clone https://github.com/xxxr/Repository.git $ cd Repository/ $ git config --global us ...
- css折叠表格
1.html <div class="custom-fold-table"> <table cellpadding="0" cellspaci ...