自动售货系统#完全靠自己做出来的,太不容易了!!!!!!!!
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
from re import split # 商品信息表:商品名称、价格,初始数量为0 goods={ 'A1':[2,0], 'A2':[3,0], 'A3':[4,0], 'A4':[5,0], 'A5':[8,0], 'A6':[6,0] } good_name=list(goods.keys()) # 存钱盒 box={} # 投币余额,初始值为0 balance=0 def initialize(r): n=r[2:].split() # 空格分隔,前面是商品数量,后面是钱币数量 goods_n=list(map(int,n[0].split('-'))) money_n=list(map(int,n[1].split('-'))) for i in range(6): goods[good_name[i]][1]=goods_n[i] box[10]=money_n[3] box[5]=money_n[2] box[2]=money_n[1] box[1]=money_n[0] print('S001:Initialization is successful') def pay(p): global balance #python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,对该变量的引用自然就会出现没定义这样的错误,需要声明其为全局变量 p_split=p.split() value=int(p_split[1]) total=0 for i in range(6): total += goods[good_name[i]][1] if value not in [10,5,2,1]: print('E002:Denomination error') elif value==5 or value==10: if value > box[1]*1+box[2]*2: print('E003:Change is not enough, pay fail') else: if total==0: print('E005:All the goods sold out') else: box[value] += 1 # 投币成功后,对应面值的数量要+1 balance += value # 投币余额 print('S002:Pay success,balance=%d'%balance) else: if total==0: print('E005:All the goods sold out') else: box[value] += 1 # 投币成功后,对应面值的数量要+1 balance += value # 投币余额 print('S002:Pay success,balance=%d'%balance) def buy(b): # 一次只能买一样,可以多次购买 b_split=b.split() staff=b_split[1] global balance if staff not in good_name: print('E006:Goods does not exist') elif goods[staff][1]==0: print('E007:The goods sold out') elif balance < goods[staff][0]: print('E008:Lack of balance') else: balance -= goods[staff][0] # 若可以购买,则余额减去物品的价格 print('S003:Buy success,balance=%d'%balance) def return_coin(c): global balance if balance==0: print('E009:Work failure') else: box1={} for key,value in box.items(): if value!=0 and balance >= key: box1[key]=min(balance//key,box[key]) #若balance//key超出key面值的数量,则 balance -= key*(box1[key]) else: box1[key]=0 #print(box1) for key,value in sorted(box1.items()): print('%d yuan coin number=%d'%(key,value)) box[key] -= value #存钱盒内,相应的面值数量要减少 def query(q): if q[1]!=' ' or q[2]!=0 or q[2]!=1: print('E010:Parameter error') else: if q[2]==0: for key,value in goods.items(): print(key,value[0],value[1]) if q[2]==1: for key,value in sorted(box.items()): print('%d yuan coin number=%d'%(key,value)) s=input().split(';') s.remove('') # 删掉最后一个空白元素 #print(s) for i in s: if i[0]=='r': initialize(i) if i[0]=='p': pay(i) if i[0]=='b': buy(i) if i[0]=='c' and len(i)==1: return_coin(i) if i[0]=='q': query(i) ''' r 26-3-26-2-14-10 2-1-15-18;p 5;c;c;p 2;c;b A4;c;q1;q0;p 2;b A4;p 5;q0;c;q0;q1;q0;c;c;p 10;p 1;q0; S001:Initialization is successful E003:Change is not enough, pay fail E009:Work failure E009:Work failure S002:Pay success,balance=2 1 yuan coin number=0 2 yuan coin number=1 5 yuan coin number=0 10 yuan coin number=0 E008:Lack of balance E009:Work failure E010:Parameter error E010:Parameter error S002:Pay success,balance=2 E008:Lack of balance S002:Pay success,balance=7 E010:Parameter error 1 yuan coin number=0 2 yuan coin number=1 5 yuan coin number=1 10 yuan coin number=0 E010:Parameter error E010:Parameter error E010:Parameter error E009:Work failure E009:Work failure E003:Change is not enough, pay fail S002:Pay success,balance=1 E010:Parameter error '''