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 ...
随机推荐
- qt获取磁盘序列号
#include "windows.h" QString lpRootPathName = "c:\\"; LPTSTR lpVolumeNameBuffer= ...
- [ES2017] Iterate over properties of an object with ES2017 Object.entries()
The Object.entries() function is an addition to the ECMAscript scpec in Es2017. This allows us to it ...
- 开创学习的四核时代-iTOP-4412开发板开源硬件平台
iTOP-4412开发板如今比較热门的开发板.笔者最近入了一套. 也推荐给初学ARM的朋友学习,4412开发板搭载三星Exynos四核处理器,配备1GB内存,4GB固态硬盘EMMC存储,兼具高速读取与 ...
- 在 Android 应用程序中使用 SQLite 数据库以及怎么用
part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...
- JavaFX 一 出生新手村(阅读小规则)
我就不讲IDE怎么装的,网上有的是,我仅仅是说说我学习过程中遇到的,该注意的东西 1.JavaFX刚開始出是基于脚本script开发的语言,所以网上会有流传比較多关于script的JavaFX,对于被 ...
- Spark Tachyon实战应用(配置启动环境、运行spark和运行mapreduce)
Tachyon实战应用 配置及启动环境 修改spark-env.sh 启动HDFS 启动Tachyon Tachyon上运行Spark 添加core-site.xml 启动Spark集群 读取文件并保 ...
- 【深入篇】Andorid中常用的控件及属性
TextView android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/al ...
- JS错误记录 - 右侧悬浮框 - 缓冲运动
本次练习错误总结: 1. 正确: startMove( document.documentElement.clientHeight - oDiv.offsetHeight + scrollTop); ...
- factor---将素数分解为质数
- matplotlib散点图笔记
定义: 由一组不连续的点完成的图形 散点图: 包含正相关性,负相关性和不相关性. 散点图生成函数: plt.scatter(x,y) 演示代码如下: import numpy as np import ...