这个程序的效果是能显示出编号、书名和价格,如果你按下'A'按钮就算加购,而且你购买数量超出库存的话,电脑会显示提示语然后重新选择,没超出库存就继续运行。如果你按下'Q'按钮就是结算总金额了。
python源代码为:
print('---欢迎使用图书购买助手---')
bookList = ['安徒生童话','丑小鸭','拇指姑娘','卖火柴的小女孩','龟兔赛跑']
priceList = [53,35,23,25,45]
numList = [1000,900,688,229,900]
length = len(bookList)
for i in range(length):
print('{} {}---{}元'.format(i,bookList[i],priceList[i]))
total = 0
while True:
choice = input('请选择操作 : A:加购 , Q:结算 : ')
if choice == 'A':
n = int(input('输入购买的编号:'))
print(bookList[n],numList[n],'本')
num = int(input('输入购买的数量:'))
if num > numList[n]:
print('超出库存,请重新选择')
else:
numList[n] = numList[n] - num
total = total + priceList[n] * num
elif choice == 'Q':
print('总共需:',total,'元')
break