#!/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. Map集合的遍历方式:

    迭代器来遍历 : entrySet() ; keySet(); values(); eg.HashMap<String,String> map = new HashMap<Strin ...

  2. Postman使用小技巧 - 用Postman生成Request代码

    Postman这款工具可以让你很方便的测试你的Web API,那如果你实在是没办法用Postman,必须手写代码,又或者你有别的需求是Postman没法实现的,你必须写一个特殊的script或App来 ...

  3. canvas-star6.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. It English 每日更新

    unary operator 一元运算符 short circuit evaluation 短路经查询

  5. Android开发-API指南-<compatible-screens>

    <compatible-screens> 英文原文:http://developer.android.com/guide/topics/manifest/compatible-screen ...

  6. com组件 智能指针崩溃问题崩溃问题

    int main(){ CoInitialize(NULL); HRESULT hr; IWinHttpRequestPtr pHttpReq=NULL; pHttpReq.CreateInstanc ...

  7. 线程中CreateEvent和SetEvent及WaitForSingleObject的用法

    首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面. CreateEvent 函功能描述:创建或打开一个命名的或无名的事件对象. EVENT有两种状态 ...

  8. adb调试命令详解-2016.02.01

    adb(Android Debug Bridge),调试桥可以让设备的调试监测过程在远端进行,而不必在运行实际运行应用的设备上,方便调试的输出. 1 命令详解 a 查看帮助信息         adb ...

  9. C# winform编程中多线程操作控件方法

    private void Form1_Load(object sender, EventArgs e) { Thread newthread = new Thread(new ThreadStart( ...

  10. SQL Server 2008 R2 找不到 Install SQL Server Profiler 找不到 事件探查器 解决

    摘自: http://blog.csdn.net/yuxuac/article/details/8992893 SQL Server 2008 R2 Express Edition - Install ...