'''
购物小程序:
用户启动时先输入工资
用户启动程序后打印商品列表
允许用户选择购买商品
允许用户不断购买各种商品
购买时检测余额是否够,如果够直接扣款,否则打印余额不足
允许用户主动退出程序,退出时打印已购商品列表
'''
salary=input("input you salary:")
if salary.isdigit():
salary=int(salary)
else:
exit("invalid data type.....")
welcome_msg='welcome to Shopping mall'.center(50,'-')
print(welcome_msg)
product_list=[
('Iphone',5888),
('Mac Air',8000),
('Mac Pro',9000),
('XiaoMi',19.9),
('Coffee',30),
('Tesla',820000),
('Bike',900),
('cloth',200)
]
shop_car=[]
exit_flag=False
while not exit_flag: print('product list'.center(50, '-'))
#for product_item in product_list:
#p_name, p_price = product_item
'''for p_name,p_price in product_list:
print(p_name,p_price)'''
for item in enumerate(product_list):
index=item[0]
p_name=item[1][0]
p_price=item[1][1]
print(index,p_name,p_price)
user_choice=input("[q=quit,c=check]What do you want to buy?:")
if user_choice.isdigit():#肯定是选择商品
user_choice=int(user_choice)
if user_choice<len(product_list):
p_item=product_list[user_choice]
if p_item[1]<salary:
shop_car.append(p_item)#放入购物车
salary -=p_item[1]#减钱
print("Added [%s] into shop car,your current balance is [%s]"%(
p_item,salary))
else:
print("your balance is [%s],can not afford this.."%salary)
else:
if user_choice=='q' or user_choice=="quit":
print("purchased products as belows:".center(40,'*'))
for item in shop_car:
print(item)
print("End".center(40,'*'))
print("your balance is [%s]"%salary)
exit_flag=True
elif user_choice=='c' or user_choice=="check":
print("purchased products as belows:".center(40,'*'))
for item in shop_car:
print(item)
print("End".center(40,'*'))
print("your balance is [%s]"%salary)

python学习day4--python基础--购物小程序的更多相关文章

  1. 老男孩python学习之作业一购物小程序

    想学编程由来已久 始终没有个结果,痛心不已 如今再次捡起来,望不负期望,不负岁月 ......一万字的废话...... 先介绍一下我的自学课程吧 "路飞学城"的<python ...

  2. Python学习day11-函数基础(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. Python学习课程零基础学Python

    python学习课程,零基础Python初学者应该怎么去学习Python语言编程?python学习路线这里了解一下吧.想python学习课程?学习路线网免费下载海量python教程,上班族也能在家自学 ...

  4. Python学习day16-模块基础

    <!doctype html>day16 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...

  5. Python学习day05 - Python基础(3) 格式化输出和基本运算符

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  6. Python学习笔记之基础篇(-)python介绍与安装

    Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...

  7. 记录Python学习中的几个小问题

    记录Python学习中的几个小问题,和C#\JAVA的习惯都不太一样. 1.Django模板中比较两个值是否相等 错误的做法 <option value="{{group.id}}&q ...

  8. Python 练习冊,每天一个小程序

    Python 练习冊,每天一个小程序 说明:     Github 原文地址: 点击打开链接 Python 练习冊.每天一个小程序.注:将 Python 换成其它语言,大多数题目也试用 不会出现诸如「 ...

  9. python学习日记(基础数据类型及其方法01)

    数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...

随机推荐

  1. FluentData微型ORM

    最近在帮朋友做一个简单管理系统,因为笔者够懒,但是使用过的NHibernate用来做这中项目又太不实际了,索性百度了微型ORM,FluentData是第一个跳入我眼睛的词.简单的了解下FluentDa ...

  2. 数据访问层DAL(数据库访问抽象类DataProvider)

    晒晒数据访问层DAL,看看你的项目数据访问层使用的是什么形式,数据访问性能比较 采用什么样的数据访问形式是软件编码很重要的一个环节,良好的数据访问形式不仅能够提搞代码的执行效率,协作能力,更重要的是对 ...

  3. ldap

    年1月份最新的包.       安装前的准备工作 # mkdir ?p /data/packages//习惯性的把一些包放在一个位置 # yum install openldap-devel zlib ...

  4. VM中ubuntu已经正确配置了静态IP仍无法上网

    情况描述:正确配置了ubuntu的IP,getway,DNS..无法ping通getway. 环境:宿主机:win7 32Bit  虚拟机:ununtu 10.04  VM:9.0.1 build-8 ...

  5. java递归简易应用教程

    package com.xiaohao.test; import java.util.ArrayList;import java.util.List;import java.util.Locale;i ...

  6. sessionapplicationStruts2中访问web元素

    本文是一篇关于sessionapplication的帖子 取得Map类型request,session,application,实在类型 HttpServletRequest, HttpSession ...

  7. cdoj 1246 每周一题 拆拆拆~ 分解质因数

    拆拆拆~ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1246 Descri ...

  8. WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)

    一. 摘要 首先圣殿骑士非常高兴这个系列能得到大家的关注和支持.这个系列从七月份開始到如今才第七篇,上一篇公布是在8月2日,掐指一算有二十多天没有继续更新了,最主要原因一来是想把它写好,二来是由于近期 ...

  9. POJ 3026 Borg Maze

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7998   Accepted: 2675 Descrip ...

  10. 调用webservice查询手机号码归属地信息

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...