python(4)- 简单练习:python实现购物车的优化

简单版本,链接如下:

http://www.cnblogs.com/c-x-m/p/7819220.html

购物车程序优化题目要求:

1. 用户退出时打印商品列表时,按以下格式

-------您购买的商品如下------

id    商品    数量    单价    总价

1      iPhone 2      5800     11400

2      coffee  1      30      30

...

总计价格: 11430元

--------end -------------


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

salary=int(input("please input your salary:"))      #输入薪资

product_list=[["iphone"5888],                       #定义商品列表

             ["coffee",30],

             ["bike",299],

             ["vivo x9",2499],

             ["cake",40],

             ["book",99]]

product_cart={}           #定义购物车字典

total_cost= 0         #定义总花销

while True:              #循环打印可购买商品列表及退出提示

    print("可购买的商品")

    for number in range(len(product_list)):

        product = product_list[number]

        print(number,product)

    print("q","quit")

    choice=input("输入你所选择的商品编号>>:").strip()     #输入选择的商品编号或退出“q”

    if choice.isdigit():                                    #判断输入的是否为整数

        choice=int(choice)

        if choice < len(product_list) and choice >= 0:      #判断输入的数字是否商品编号范围内

            product=product_list[choice]                     #定义要买的商品列表

            if salary -product[1] >=0:                       #判断是否买的起

                salary-=product[1]                           #买的起自动结算

                print("将商品",product[0],"加入购物车,","你目前还有余额",salary,"元")   #输出当前本次操作信息及余额

                if product[0in product_cart:               #判断购买的商品是否在购物车里

                    product_cart[product[0]][1]+=1           #在购物车里,则商品对应的数量+1

                else:                                        #不在购物车里,则将商品信息,商品单价,数量加入购物车内

                    product_cart[product[0]]=[product[1],1]

                print("目前你的购物车",product_cart)

            else:            #买不起则打印当前余额及差的金额。结束本次循环,重新开始循环,打印可购买商品列表及退出提示

                print("你目前还有余额", salary, "元,""还差", product[1- salary, "元")

        else:                         #输入的数字不在商品编号范围内,返回上一循环,打印可购买商品列表及退出提示

            print("商品不存在!")

    elif choice== "q":                #输入的不是数字为字符串“q”,打印已购买商品信息及总花销

        print("---------你购买的商品如下---------")

        print("id","\t","商品","\t","数量","\t","单价","\t","总价")

        id_counter=1                        #定义购买车商品编号

        for key in product_cart:

           print(id_counter,"\t",           #依序打印购物车商品编号,商品,数量,单价及总价

                 key,"\t",

                 product_cart[key][1],"\t\t",

                 product_cart[key][0],"\t",

                 product_cart[key][1]*product_cart[key][0])

           id_counter+=1

           total_cost+=product_cart[key][1]*product_cart[key][0]          #定义总花销

        print("总计价格为",total_cost)

        print("------------end------------")

        break                                                          #跳出循环

    else:             #输入的既不是整数,也不是字符串“q”,提示信息错误

        print("你输入的是什么鬼?")

  

运行程序结果为:

时间: 2024-08-30 05:51:47

python(4)- 简单练习:python实现购物车的优化的相关文章

python实现简单的循环购物车小功能

python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s", 5000), ("book python", 81), ("iwach", 3200), ("电视机", 2200) ] def zero(name): if len(name) == 0: print("\033[31;1m您的输

python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器

python之simplejson,Python版的简单. 快速. 可扩展 JSON 编码器/解码器 simplejson Python版的简单. 快速. 可扩展 JSON 编码器/解码器 编码基本的 Python 对象层次结构: import simplejson as json print json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) print json.dumps("\"foo\bar") print json

python psutil简单示例

利用psutil编写简单的检测小脚本 0.安装psutil模块                                                                                            psutil官网:https://pythonhosted.org/psutil/ 下载: # wget https://pypi.python.org/packages/57/93/47a2e3befaf194ccc3d05ffbcba2cdcdd22

python中简单文件的输入三种方式

最近在自学python,简单的总结了一下文件的输入的方式. 1. f=open("foo.txt") line=f.readline() while line: print(line,end='') line=f.readline() f.close() 2. for line in open("foo.txt"): print(line,end='') 3. f=open("foo.txt") lines=f.readlines() for l

作业1开发一个简单的python计算器

开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致 hint: re.search(r'\([^()]+\)',s).group() '(-

Python实现简单的猜数字游戏

Python实现简单的猜数字游戏,具体如下: 随机生成一个1-10之间的数字,让用户来猜,当猜错时,会提示猜的数字是大还是小了,直到用户猜对为止. import random secret = random.randint(1,10) #print(secret) print('------猜数字游戏!-----') guess = 0 while guess != secret: temp = input('猜数字游戏开始,请输入数字:') guess = int(temp) if guess

用 python实现简单EXCEL数据统计

任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_workbook('demo.xlsx') #打开excel数据表 SheetList = workbook.sheet_names()#读取电子表到列表 SheetName = SheetList[0]#读取第一个电子表的名称 Sheet1 = workbook.sheet_by_index(0)

python 实现简单排序

今天偶得一本神奇的算法秘笈,据编辑说是一本easy and intresting 的书,所以我就开始翻开了. 书中作者用的是C语言,我最近正啃python 所以想着用python来解决作者的提问. 这不,第一页就来了,这对于人脑来说是一个很简单的,但是对于将学业知识全还给老师的人来说还是想了老半天,查了好一会儿的资料的. 经过思考,我选定了数组,然后搜索了python关于数组的知识,结果,貌似python不太重视数组,或者说法不一样,后来看了list和dict,觉得应该类似的,参考后,也就出来了

简单的python爬虫 爬的乌云漏洞类型

import urllib.request import re starturl="http://wooyun.org/searchbug.php?q=%E9%87%91%E8%9E%8D" def get_html_response(url): html_response = urllib.request.urlopen(url).read().decode('utf-8') return html_response def geturl(starturl): a=get_html_