python 自动售货机

cunqianhe = {1:2,2:2,5:1,10:1}    #存钱盒
shangpin = [
['A1',2,2],
['A2',3,2],
['A3',4,1],
['A4',5,2],
['A5',8,2],
['A6',6,2],
]                              #商品信息 商品名 价格 数量
a = 0
for i in shangpin:
    a += i[2]

投币函数

def p (R):
toubiyue = 0
u = cunqianhe[1] + cunqianhe[2]*2
if int(R) not in [1,2,5,10]:
    print("E002:Denonmination error")
else:
    if int(R) > 2 and u < int(R):
        print("E003:Change is not enough,pay fail")
    elif int(R)> 10:
        print("E004:Pay the balance is beyond the scope biggest")
    elif a == 0:
        print("E005:All the goods sold out")
    else:
        for i in cunqianhe.keys():
            if int(R) == i:
                toubiyue += i
                cunqianhe[i] += 1
        print("S002:Pay success,balance=%s" % (R))
return toubiyue

购买函数

def b (S):
X = 0
for i in shangpin:
    if S == i[0]:
        X = i[1]
        i[2] = i[2] -1
        break
shangpinming = []
for i in shangpin:
    shangpinming.append(i[0])
if S not in shangpinming:
    print("E006:Goods does not exist")
elif a == 0:
    print("E007:The goods sold out")
else:
    print("S003:Buy success,balance=%d" %(X))
return X

查询函数

def q (x):
d = sorted(shangpin,key=lambda x: -x[2])
if x == '0':
    for i in d:
        a = 0
        for k in i:
            i[a] = str(k)
            a += 1
        print(' '.join(i))
elif x == '1':
    for k,v in cunqianhe.items():
        print("%d yuan coin number=%d" %(k,v))
else:
    print("EO1O:Parameter error")

退币函数

def c (d,X):
a = []
b = []
tuibi = d - X
p = 0
m =0
for i in cunqianhe.values():
    b.append(i)
for k,v in cunqianhe.items():
    if v != 0:
        cunqianhe[k] = v - tuibi / k
        tuibi = tuibi % k
for i in cunqianhe.values():
    a.append(b[p] - i)
    p += 1
for i in cunqianhe.keys():
    print("%d yuan coin number=%d" %(i,a[m]))
    m += 1

print(“””
A1 2元
A2 3元
A3 4元
A4 5元
A5 8元
A6 6元
“”“)
x = 0
jiage = 0
e = 0

主程序

while True:
a = input()
zhiling = a.split(' ')

投币

if zhiling[0] == 'p' and len(zhiling) ==2:
    x = p(zhiling[1])

购买

if zhiling[0] == 'b' and len(zhiling) == 2:
    for i in shangpin:
        if i[0] == zhiling[1]:
            jiage = i[1]
    if x < jiage :
        print("E008:Lack of balance")
    else:
        x = x - jiage
        e = b(zhiling[1])

查询

if zhiling[0] == 'q' and len(zhiling) == 2:
    d =  q(zhiling[1])

if zhiling[0] == 'c' and len(zhiling) == 1:
    if x == 0:
        print("E009:Work failure")
    if x > 0:
        c(x,e)

初始化

if zhiling[0] == 'r' and len(zhiling) == 3:
    chushishangpin = zhiling[1].split('-')
    shangpinchang = len(shangpin)
    a = 0
    w = 0
    for k in shangpin:
        k[2] = int(chushishangpin[a])
        a += 1
    chushiqianshu = zhiling[2].split('-')
    for i in cunqianhe.keys():
        cunqianhe[i] = int(chushiqianshu[w])
        w += 1
    print("S001:Initialization is successful")

猜你喜欢

转载自blog.csdn.net/zcx1203/article/details/82083321