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 ...
随机推荐
- C#中一些字符串操作的经常使用使用方法
概述 本篇主要解说,字符串string的基本操作知识 倒序输出 string str = Console.ReadLine(); for (int i = str.Length - 1; i > ...
- Codeforces 240E. Road Repairs 最小树形图+输出路径
最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 对string的一些扩展函数
对string作了一些扩展,包含string转化为int.string转化为double.string转化为bool.打印系统当前时间.但没有解决数据溢出的问题,请大神帮忙解决! //头文件 /*pa ...
- 华为荣耀3C最新版ROM的root,(4.7.1和4.8.1等等通用方法)
手头一台honor 3c的机器.应该是线刷的时候,把IMEI给刷掉了.导致移动2G卡无法上网. 刷了4.7.1或者4.8.1.尝试了全部方法都root失败了. 正好我手头有6582的代码.我想.既然系 ...
- 停止复制代理后AWT缓存组的行为
AWT缓存组中尽管大多数时候数据是从TimesTen到Oracle,但也存在数据从Oracle到TimesTen的情形. 数据从TimesTen下沉到Oracle依靠复制代理,数据从Oracle到Ti ...
- HTML5学习笔记之二CSS基础
一般来说,CSS都存储为一个文件.然后各个html page能够指定使用哪个CSS文件.这样这些html页面就能够保持一致的风格. 通常能够通过在head中加上一行指定CSS的链接. <!DOC ...
- 查看activity task相关信息
可以使用命令 adb shell dumpsys activity 查看的结果如下 ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents ...
- JS --- 延迟加载的几种方式
标题:JS延迟加载,也就是等页面加载完成之后再加载 JavaScript 文件. JS延迟加载有助于提高页面加载速度. 1. defer 属性 HTML 4.01 为 <script>标 ...
- 120.VS调试技巧
设置断点调试 在一行代码的左侧点击即可设置断点,按F5(调试->开始调试)即可运行到第一个端点处暂停 逐语句调试 按F11(调试->逐语句)即可开始一步一步执行 逐过程调试 按F10(调试 ...
- Eclipse怎样把文件系统形式的项目作为工程直接导入?
导入的时候,选择已经存在的工程,如果选择文件系统,可能会提示没有项目可以导入.这个时候,可以从其它Eclipse项目下,copy一份.project文件过来,修改源文件中的工程名字.如果需要,也可以c ...