首页 > 代码库 > python 3.5 购物小程序

python 3.5 购物小程序

 1 #!/usr/bin/env python
 2 #encoding: utf-8
 3 import time
 4 import os
 5 
 6 nowtime = time.strftime(%Y-%m-%d %H:%M:%S)
 7 shopping_total_money = 0
 8 shopping_car = {}
 9 
10 
11 def touch_shopping_log():
12     with open(shopping_log.txt, w) as shopping_log:
13         pass
14 
15 def shopping_log_add(log):
16     with open(shopping_log.txt, a) as shopping_log:
17         shopping_log.write(%s : %s : %s\n % (nowtime,name,log))
18 
19 shopping_list = [
20     ["phone",100],
21     ["computer",200],
22     ["car",1000],
23     ["home",10000],
24 ]
25 def welcome_info():
26 
27     print (_________________________________)
28     print (welcome to shopping 北京购物广场)
29     print (now time is : %s % (nowtime))
30     print (_________________________________)
31 
32 
33 def shopping_list_info():
34     for index,info in enumerate(shopping_list,0):
35         print (%s | name: %s | money: %s % (index,info[0],info[1]))
36 
37 
38 def shopping_start():
39     while True:
40         shop_num = int(input(please input number [88:quit]:))
41         if shop_num == 88:
42             break
43         shopping_car[shopping_list[shop_num][0]] = shopping_list[shop_num][1]
44 
45 def shopping_stop():
46     global  shopping_total_money
47     print (.............shopping list open...........)
48     for k,v in shopping_car.items():
49         print (name: %s money: %s % (k,v))
50         shopping_total_money += v
51     print(............shopping list end.......)
52     print (total cost   [%s]    yuan % (shopping_total_money))
53 
54 def shopping_close():
55     global shopping_total_money
56     make_sure = input(make sure [y|n])
57     if make_sure == y:
58         print (thank you come on)
59         for k, v in shopping_car.items():
60             log_info = name: %s money: %s % (k, v)
61             shopping_log_add(log_info)
62         shopping_total_money = 0
63         shopping_car.clear()
64 
65     elif make_sure == n:
66         print (your no make sure)
67         print (clear shopping car .....)
68         shopping_total_money = 0
69         shopping_car.clear()
70 
71 def shopping_run():
72     global  name
73     welcome_info()
74     shopping_list_info()
75     print (________________________________)
76     name = input(inout your name:)
77     shopping_start()
78     shopping_stop()
79     shopping_close()
80 
81 if __name__ == "__main__":
82     while True:
83         shopping_run()
84         time.sleep(2)
85         os.system(cls)

记录日志信息:

1 2016-10-30 09:21:40 : beijing : name: car money: 1000
2 2016-10-30 09:21:40 : beijing : name: computer money: 200
3 2016-10-30 09:21:40 : beijing : name: home money: 10000
4 2016-10-30 09:21:40 : shanghai : name: phone money: 100
5 2016-10-30 09:21:40 : shanghai : name: car money: 1000
6 2016-10-30 09:21:40 : shanghai : name: computer money: 200

 

python 3.5 购物小程序