智一面的面试题提供python的测试题
http://www.gtalent.cn/exam/interview?token=f098f775ffa0106d0451f7fd97357f23
购物清单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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# 定义仓库 repository = dict() # 定义购物清单 shop_list = [] # 初始化商品 def init_repostory(): goods1 = ( "001" , "Python" , 66 ) goods2 = ( "002" , "C++" , 88 ) goods3 = ( "003" , "Linux" , 90 ) goods4 = ( "004" , "MySql" , 35 ) # 商品入库 repository[goods1[ 0 ]] = goods1 repository[goods2[ 0 ]] = goods2 repository[goods3[ 0 ]] = goods3 repository[goods4[ 0 ]] = goods4 # 商品清单 def show_goods(): print( "欢迎光临,欣悦超市" ) print( "商品清单:" ) print( '%13s%40s%10s' % ( "条码" , "商品名" , "单价" )) # 遍历 for goods in repository.values(): print( '%15s%40s%12s' % goods) # 显示购物清单 def show_list(): print( "=" * 100 ) # 输出 if not shop_list: print( "未购物" ) else : title = '%-5s|%15s|%40s|%10s|%4s|%10s' % \ ( "ID" , "条码" , "商品名" , "单价" , "数量" , "小计" ) print(title) print( "-" * 100 ) # 总计 sum = 0 # 遍历清单 for i, item in enumerate(shop_list): id = i + 1 code = item[ 0 ] name = repository[code][ 1 ] price = repository[code][ 2 ] number = item[ 1 ] amount = price * number sum = sum + amount line = '%-5s|%17s|%40s|%12s|%6s|%12s' % \ (id, code, name, price, number, amount) print(line) print( "-" * 100 ) print( " 总计:" , sum) print( "=" * 100 ) # 添加购买商品 def add(): code = input( "输入条码:\n" ) if code not in repository: print( "条码错误" ) return # 找商品 goods = repository[code] # 数量 number = input( "购买数量:\n" ) shop_list.append([code, int (number)]) # 修改数量 def edit(): id = input( "修改的ID: \n" ) index = int (id) - 1 item = shop_list[index] # 新的数量 number = input( "新的购买数量:\n" ) item[ 1 ] = int (number) # 删除 def delete(): id = input( "删除的ID:" ) index = int (id) - 1 del shop_list[index] def payment(): show_list() print( '\n' * 3 ) print( "下次光临" ) # 退出 import os os._exit( 0 ) cmd_dict = { 'a' : add, 'e' : edit, 'd' : delete, 'p' : payment, 's' : show_goods} #命令提示 def show_command(): cmd = input( "输入操作指令:\n" + " 添加(a) 修改(e) 删除(d) 结算(p) 超市商品(s)\n" ) if cmd not in cmd_dict: print( "按指示输入" ) else : cmd_dict[cmd]() init_repostory() show_goods() while True: show_list() show_command() |