# Author:Eric Zhao
# -*- coding:utf-8 -*-
'''需求:
启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额''' product_list = [
('IPhone',5000),
('Bike', 500),
('Car', 50000),
('Hat', 50)
]
shopping_list = []
salary = input('Please input your salary..')
if salary.isdigit():
salary = int(salary)
while True: # 死循环
# for item in product_list:
# print(product_list.index(item),item)
for index,item in enumerate(product_list):
print(index,item) user_choice = input('Please type a product number,if type q then exit..')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice>=0:
choice_list = product_list[user_choice]
if salary >= choice_list[1]: # 买得起
shopping_list.append(choice_list)
salary = salary - choice_list[1]
print('Added %s into shopping cart,your balance is\033[31;1m%d\033[0m'%(choice_list,salary))
else:
print('Your balance is only \033[31;1m%d\033[0m,not enough..'%salary)
else:
print('The product number [\033[31;1m%d\033[0m] doesn\'t exist..'%user_choice)
elif user_choice == 'q':
print('------------------- shopping list --------------------')
for mylist in shopping_list:
print(mylist)
print('Your balance is \033[31;1m%d\033[0m'%salary)
exit()
else:
print('Invalid number')
else:
print('Please type a integer..')

Python 简单购物程序的更多相关文章

  1. python ATM购物程序

    需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...

  2. Python简单小程序练习

    1.九九乘法表 #!/usr/bin/python for i in range(1,10): for j in range(i): j += 1 print ("%d * %d = %-2 ...

  3. python简单小程序

    #足球队寻找10 到12岁的小女孩(包含10岁和12岁),编写程序询问用户性别和年龄,然后显示一条消息指出这个人是否可以加入球队,询问10次,输出满足条件的总人数#询问10次,输出满足要求的总人数 o ...

  4. 用python开发简单ftp程序

    根据alex老师视频开发的简单ftp程序,只能实现简单的get功能 ftp客户端程序: #!/usr/bin/env python #_*_ coding:utf-8 _*_ import socke ...

  5. python简单的监控脚本-利用socket、psutil阻止远程主机运行特定程序

    python简单的监控脚本-利用socket.psutil阻止远程主机运行特定程序 psutil是一个跨平台的库(http://code.google.com/p/psutil/),能够轻松的实现获取 ...

  6. 简单购物车程序(Python)

    #简单购物车程序:money_all=0tag=Trueshop_car=[]shop_info={'apple':10,'tesla':100000,'mac':3000,'lenovo':3000 ...

  7. [terry笔记]python购物程序

    如下是一个购物程序: 先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品. count = 0 while True: #做一个循环判断,如果输入的不是数字,基于提示,三次后退出 ...

  8. Python简单爬虫入门二

    接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...

  9. GJM : Python简单爬虫入门(二) [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

随机推荐

  1. 微服务的.NET Core示例框架

    eShopOnContainers 是一个基于微服务的.NET Core示例框架 https://www.cnblogs.com/fengqingyangNo1/p/9438428.html 找到一个 ...

  2. openstack安装newton版本keyston部署(一)

    一.部署环境:     两台centos7,  内存2G 控制计算节点: Hostname1:                    ip:172.22.0.218 计算节点及存储节点 Hostnam ...

  3. scp 可以在 2个 linux 主机间复制文件

    Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...

  4. arcgis python 保存当前窗口图形为jpg

    1,第一步打开arcgis 将图形加载进去 第二步,将要保存的图形调到合适的比例尺,然后点击下面按钮 第三步,将写好的python 语句放到里面去: import arcpy mxd = arcpy. ...

  5. 合并石子,区间dp

    #define INF 9999999 ],dp[][],ans=,s[]; int main() { scanf("%d",&n); ;i<=n;i++) scan ...

  6. Linq 根据list属性去重复

    s.Where((x, i) => s.FindIndex(z => z.ArticleTitle == x.ArticleTitle) == i).ToList();

  7. ubuntu 14.04 安装minidwep-gtk

    一,安装相关的依赖 sudo apt-get install build-essential libssl-dev iw libpcap-dev sqlite3 libsqlite3-dev libp ...

  8. JavaScript中var a=b=c=d的发现

    看了别人的博客随手记录下 先看一下以下的代码 var a=1,b=2,c=3; (function(){ var a=b=1; })(); console.log(a); console.log(b) ...

  9. cssText在js中写样式表兼容全部

    oDiv.style.cssText="width:100px;height:200px;";是前面的升级版(oDiv.style.width='200px';) <styl ...

  10. C语言的time函数和localtime函数

    1.获取当前时间,并获取当前时间(即系统时间)距离1970年1月1日的时间间隔,以秒为单位. 2.获取指定时间距离1970年1月1日的时间间隔,以秒为单位.