本文主要给大家介绍了关于python实现循环购物车功能的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:
示例代码
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
53
54
55
56
57
58
59
60
|
# -*- 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您的输入不能为空,清重新输入~~\033[0m" ) exit() else : return True def crre(name): if int (name) not in range ( len (shopping)): print ( "\033[31;1m您输入了错误的序列号,请重新输入~~\033[0m" ) exit() else : return True gongzi = input ( "请输入您目前的工资:" ) if not gongzi.isdigit(): crre() elif len (gongzi) = = 0 : zero() else : gongzi = int (gongzi) print ( "=========================================" ) print ( "===要购买的列表清单如下: " ) for i,j in enumerate (shopping): print (i, j[ 0 ], j[ 1 ]) shopping_cart = [] while True : choice = input ( "请输入你要购买的商品,输入q或者exit退出选择回到购物车列表: " ) if choice.isdigit(): zero(choice) crre(choice) choice = int (choice) goods = shopping[choice] if gongzi < int (goods[ 1 ]): gongzi - = goods[ 1 ] print ( "===您的工资余额不够使用了,还差\033[1;31m{}\033[0m请努力挣钱===." . format ( abs (gongzi))) else : print ( "您选购的商品:\033[1;32m %s \033[0m 已经加入到购物车.." % (goods[ 0 ])) shopping_cart.append(goods) gongzi - = goods[ 1 ] print ( "您的工资余额还剩下:\033[30;42m %s\033[0m" % (gongzi)) elif (choice = = "q" ) or (choice = = "exit" ): break print ( '\033[30;42m你已购商品列表\033[0m' .center( 42 , '-' )) for i,j in enumerate (shopping_cart): print ( "\033[1;32m" + str (i + 1 ) + " " + str (j[ 0 ]) + " " + str (j[ 1 ]) + "\033[0m" ) |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://www.cnblogs.com/hujianli/p/7118957.html