Day2:购物车小程序
一、购物车小程序第一版
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
product_list = [
("iphone 8", 5888),
("Mi Note3", 2499),
("Bike", 788),
("Phthon 3.5", 59),
("Mac pro", 9888),
("Honor 6", 599)
]
notfalse = not False
shopping_cart = []
while notfalse:
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary) while True:
# for i in product_list:
# print(product_list.index(i),i)
for index, i in enumerate(product_list):
print(index, i) # 打印商品列表
user_choice = input("Please choose what you need to buy:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0: # 用户选择了对应的商品
if product_list[user_choice][1] <= salary: # 买得起
salary = salary - product_list[user_choice][1] # 扣钱
shopping_cart.append(product_list[user_choice])
print("You have purchased %s, and your current balance is %s." % (
product_list[user_choice], salary))
elif user_choice == "": # 直接按的回车,没有输入
print("Please enter the corresponding number of the goods!")
else: # 钱不够,买不起!
print("Your balance is not enough!")
else: # 用户输入的数字超出商品总数
print("Please check the commodity number!")
elif user_choice == "q" or user_choice == "Q": # 要退出
print("------------shopping list-------------")
for index, i in enumerate(shopping_cart):
print(index, i)
print("Your current balance is %s." % salary)
exit()
else: # 用户输入的不是数字
print("Please enter the corresponding number of the goods!") elif salary == "q" or salary == "Q": # 用户想退出
notfalse = False
else: # 用户输入了其他字符
print("Please input your salary by number!")
Day2:购物车小程序的更多相关文章
- Python之路 day2 购物车小程序1
#Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...
- python 购物车小程序
python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...
- [作业] Python入门基础---购物车小程序
1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...
- python3 购物车小程序,余额写入文件保存
python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...
- python编写购物车小程序
#练习#程序购物车#启动程序后,让用户输入工资, 然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额 ...
- python写购物车小程序
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Author: Skyell Wang # @Time : 2018/5/22 15:50 # 基础要 ...
- python 练习购物车小程序
# -*- coding:utf-8 -*- shp = [ ['iphone',5000], ['offee',35], ['shoes',800] ] pric_list = [] e = int ...
- 购物车小程序(while循环,列表)
while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...
- python 购物车小程序(列表、循环、条件语句)
goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...
随机推荐
- HH生病了(hpu1136)
HH生病了 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 324 Solved: 90 [Submit][Status][Web Board] De ...
- 游戏开发之UDK引擎介绍和模型导入
2014-09-18 10:01:3 3.7.5" style="border:0px; vertical-align:middle; max-width:100%"&g ...
- HTML5学习笔记之二CSS基础
一般来说,CSS都存储为一个文件.然后各个html page能够指定使用哪个CSS文件.这样这些html页面就能够保持一致的风格. 通常能够通过在head中加上一行指定CSS的链接. <!DOC ...
- HTTP -- 请求/响应 结构
一:一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据. 1.请求行 1.请求方法:GET POST 2.URL字段 3.HTTP版本字段 2.请求头 1.Accept:浏览器可接受 ...
- MathType下载和安装(与Visio搭配使用)
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- 洛谷P1908 逆序对(归并排序)
题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...
- docker 部署 jenkins server
1. 拉取一个jenkins 镜像 docker pull jenkins 2. 创建与jenkins配置目录对应的,容器外的,文件目录,并修改相应的权限 mkdir /home/jenkins ch ...
- 洛谷 P1105 平台
P1105 平台 题目描述 空间中有一些平台.给出每个平台的位置,请你计算从每一个平台的边缘落下之后会落到哪一个平台上.注意,如果某两个平台的某个两边缘横坐标相同,物体从上面那个平台落下之后将不会落在 ...
- Vue 学习记录<2>
一.Vue https://vue-loader.vuejs.org/zh-cn/ https://vuejs-templates.github.io/webpack/structure.html
- AIX中经常使用的SMIT 的使用
AIX中经常使用的SMIT 的使用 1. smit 的日志文件 (1)$HOME/smit.log 记录了所訪问的全部菜单.对话内容,所运行的命令和输出结果 在 SMIT 会话中出现的全部 ...