#!/usr/bin/env python
# -*- coding: utf-8 -*- #如下是一个购物程序:
#先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品。
count = 0
while True: #做一个循环判断,如果输入的不是数字,基于提示,三次后退出
salary = input("input your salary:") #输入你的工资
if salary.isdigit(): #输入的工资必须是数字才能往下走
salary=int(salary) #转换为整数型
break
else:
print("Please input a number:")
count += 1
if count == 3:
exit() goods_list = [["Iphone",5800],["Macbook",12800],["iMac",15000],["ApplePen",500],["IPod",1200]] #商品列表
shop_list = [] #购买的商品列表
msg = " Product List "
print(msg.center(30,"*"))
for i,ele in enumerate(goods_list): #遍历序列中的元素以及它们的下标
print(i,".",ele[0],ele[1])
while True:
choice = input("Which do you want(quit type \"quit\")>>>")
if choice.isdigit(): #判断选择的是否是数字
choice = int(choice) #转换为整数型
if choice <= len(goods_list)-1: #选择的数字必须小于列表长度-1,因为下标从0开始
if salary >= int(goods_list[choice][1]): #判断工资是否够
shop_list.append(goods_list[choice]) #够的话,把商品加入到shopList
salary -= goods_list[choice][1] #减去工资
print("You just buy a %s now you have %s RMB" % (goods_list[choice][0],salary))
else:
print("Not enough money")
else:
print("There is no such things")
elif choice == "quit":
print("Here is what you buy:") #这里的思路是,创建一个字典,把所买的商品格式化输出
total = 0
shop_dict={}
for item in shop_list:
things = item[0]
money = item[1]
total += int(money)
if things in shop_dict:
shop_dict[things][0] += 1
shop_dict[things][1] += money
else:
shop_dict[things]=[1,money]
for item in shop_dict.items():
print("%s %s个 共%s" % (item[0],item[1][0],item[1][1]))
print("一共花了:",total)
exit()
else:
print("Please input a number")
continue

python杂记-3(购买商品)的更多相关文章

  1. 【Python】唯品会购买商品

    操作过程:唯品会进入之后,搜索商品,浏览网页,略掉不能选择的尺寸,选择之后,点击商品选择数量的加号,然后加入购物车. 实现代码如下: # coding=utf-8 from selenium impo ...

  2. python入门之实例-购买商品

    需求: 选择商品,结算所选的商品 #目前总资产 asset_all = 0 #所选商品总价 all_price = 0 #购物车列表,目前已选择商品 #每个元素的结构:"商品名": ...

  3. Python之爬虫-京东商品

    Python之爬虫-京东商品 #!/usr/bin/env python # coding: utf-8 from selenium import webdriver from selenium.we ...

  4. C 购买商品的游戏

    1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 /* 5 *模拟实现道具店购物 ...

  5. Java初学者作业——编写Java程序, 实现根据用户购买商品总金额, 计算实际支付的金额及所获得的购物券金额。

    返回本章节 返回作业目录 需求说明: 编写Java程序, 实现根据用户购买商品总金额, 计算实际支付的金额及所获得的购物券金额. 购买总金额达到或超过 1000元,按 8折优惠,送 200元的购物券: ...

  6. 编写Java程序,用户在网上购买商品(good),当用户买了一本书(book)、一顶帽子(hat)或者买了一双鞋子(shoe),卖家就会通过物流将商品邮寄给用户,使用简单工厂模式模拟这一过程。

    查看本章节 查看作业目录 需求说明: 编写Java程序,用户在网上购买商品(good),当用户买了一本书(book).一顶帽子(hat)或者买了一双鞋子(shoe),卖家就会通过物流将商品邮寄给用户, ...

  7. Python基础-三次用户验证登录购买商品程序

    需求: 一:三次登录锁定 1.用户信息存放于文件中 2.尝试三次都失败,锁定用户 二.购物车功能要求: 要求用户输入总资产,例如:2000显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品 ...

  8. 618购物节要到了,Python帮你实现商品有货的微信提醒

    时间过的真快,不知不觉一年已过了一半,又快到618购物狂欢节了,剁手党们都希望无论是618购物节还是双11购物节,都能够买到便宜又实惠的商品,但是真心实惠的东西往往是紧俏的,经常会出现无货的情况,我们 ...

  9. python 获取淘宝商品信息

    python cookie 获取淘宝商品信息 # //get_goods_from_taobao import requests import re import xlsxwriter cok='' ...

随机推荐

  1. 自动以读写方式挂载ntfs-黑苹果之路

    在mac下ntfs分区总是以只读方式挂载,双操作系统,尤其有些时候需要用u盘,需要手工umount再mount,很麻烦,找了一些资料,通过修改/etc/fstab文件无效,要么就是使用第三方软件,不合 ...

  2. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

  3. Google Protocol Buffer 的编码方式

    Google Protocol Buffer 使用到了两种编码方式:Varints 和 zigzag. 一 Varints 编码 每个 byte 只用 7bit 表示数字,最高位 bit作为标志位,如 ...

  4. 解决VS2008 开发Windows Mobile 项目生成速度慢的问题

    最近用VS2008开发Windows Mobile程序,使用C#..NET Compact Framework,发现项目生成速度比较慢.用VS2008打开项目后,开始一段时间生成速度还能忍受,时间一长 ...

  5. 目标检测--Rich feature hierarchies for accurate object detection and semantic segmentation(CVPR 2014)

    Rich feature hierarchies for accurate object detection and semantic segmentation 作者: Ross Girshick J ...

  6. Android layout属性大全

    第一类:属性值 true或者 false  android:layout_centerHrizontal 水平居中      android:layout_centerVertical 垂直居中   ...

  7. 2013款Mac Pro“神秘”主机详解

    2013款Mac Pro"神秘"主机详解 自2013年12月下旬新款Mac Pro在美国发布以来,经过一个月的等待终于在国内已到货,笔者亲自测试了这个号称"神秘" ...

  8. C++: std::string 与 Unicode 结合

    一旦知道 TCHAR 和_T 是如何工作的,那么这个问题很简单.基本思想是 TCHAR 要么是char,要么是 wchar_t,这取决于_UNICODE 的值: // abridged from tc ...

  9. linux网卡绑定

  10. centos6 pxe minimal install

    # 01-78-2b-cb-69-10-f3 default menu.c32 prompt 0 timeout 100 LABEL centos-6 MENU DEFAULT MENU LABEL ...