本文共 3575 字,大约阅读时间需要 11 分钟。
# 商品属性prize_default = [2, 3, 4, 5, 8, 6]goods_info = {'A' + str(i+1): dict(prize=prize_default[i], quantity=0) for i in range(6)}# 存钱盒信息money_default = [1, 2, 5, 10]moneybox_info = {money_default[i]: 0 for i in range(4)}money_balance = 0# 投入累计cmd_list = ['r', 'p', 'b', 'c', 'q']# 命令类型cmd_in_all=[]# 命令仓refund_coin=[]# 退币仓# 命令拆分并执行def cmd_exec(): cmd_in = input() if (cmd_in[0] not in cmd_list) or not cmd_in.endswith(';'): # 输入命令不合法提示并返回 print('invalid cmd') return cmd_in_all = cmd_in.rstrip(';').split(';')# 多个命令分割 for i in cmd_in_all: # 多个命令依次执行 cmd_choice(i) cmd_in_all=[] # 清空命令仓# 输入命令并判断def cmd_choice(cmd_in): if cmd_in[0] not in cmd_list: # 命令不合法提示并返回 print('invalid cmd') return else: # 命令合法执行相应命令 cmd_def[cmd_list.index(cmd_in[0])](cmd_in.lstrip(cmd_in[0] + ' '))# 信息初始化rdef info_init(input_info): init_info = input_info.split(' ') # 从空格处断开分出两个字符串 # 限制输入数据的范围 for i in init_info: # 遍历两个字符串 for j in i.split('-'): # 从字符串中提出数字信息 if int(j) > 10: # 如果单个赋值超过10,输出提示信息 print('out of range , Initialization failed') return for i,v in zip(goods_info,init_info[0].split('-')): # 存入商品数量 goods_info[i]['quantity'] = int(v) for i,v in zip(money_default,init_info[1].split('-')): # 存入钱币数量 moneybox_info[i] = int(v) print('S001:Initialization is successful') # 初始化成功信息# 投币pdef put_in_money(money): global money_balance money = int(money) # 将字符串转成数值 if money not in money_default: # 投入不是10,5,2,1的硬币报错并返回 print('E002:Denomination error') return if money != 1 and money != 2: # 1,2硬币不适用下面规则 if money > moneybox_info[2] * 2 + moneybox_info[1] * 1: # 投入硬币大于盒内2,1硬币的总数时报错并返回 print('E003:Change is not enough,pay fail') return if money_balance > 10: # 余额大于10时报错并返回 print('E004:Pay the balance is beyond the scope biggest') return for i in goods_info: # 遍历货物数量 if goods_info[i]['quantity'] != 0: # 有货跳出循环 break else: # 没货报错并返回 print('E005:All the goods sold out') return money_balance += money # 成功投币计入余额 moneybox_info[money] += 1 # 投入币放入存钱盒 print('S002:Pay success,balance='+str(money_balance)) # 成功提示# 购买商品bdef buy(goods): global money_balance if goods not in goods_info: # 商品不存在提示并返回 print('E0:Goods does not exist') return if goods_info[goods]['quantity']==0: # 商品数量为0提示并返回 print('E007:The goods sold out') return if money_balance= i: # 当余额大于该面值硬币且该硬币数大于0时 refund_coin.append(i) # 将要退回的硬币存入列表 money_balance-=i # 余额减去对应值 moneybox_info[i]-=1 # 存钱盒内该类硬币数量减去1 else: coin_back=[refund_coin.count(i) for i in money_default] # 统计应退各类硬币数 print(coin_back) for i in range(4): # 打印退币信息 print(money_default[i],'yuan coin number='+str(coin_back[i])) refund_coin=[] # 情况退币仓# 查询库存及存钱盒信息qdef inquire(cmd): if cmd=='0': # 查询商品信息 for i in sorted(goods_info.items(),key=lambda a:a[1]['quantity'],reverse=True): print(i[0],i[1]['prize'],i[1]['quantity']) if cmd=='1': # 查询存钱盒信息 for i in money_default: # 打印退币信息 print(str(i), 'yuan coin number=' + str(moneybox_info[i]))# 命令对应函数列表cmd_def = [info_init, put_in_money, buy, refund, inquire]while True: try: cmd_exec() except Exception as error: print('system exception')
初始化:
投币:
购买:
退币:
查询:
多命令:
转载于:https://blog.51cto.com/13992211/2330207