#!/usr/bin/env python3 # py 3.6+
"""
#要求做一个系统菜单,输入数字进入对应菜单,包含以下内容,正常操作不能报错:
# 菜单1:列印所有产品价格和库存
# 菜单2:修改产品价格
# 菜单3:增加库存
# 菜单4:购买指定数量产品
# 菜单5:增加新产品 作为思考题
# 菜单0:退出当前系统
"""
price = {'vegetables': '3','eggs': '4','rice': '2'} # 价格dict
stock = {'vegetables': '0','eggs': '0','rice': '0'} # 库存dict
tip = '''
1:列印所有产品价格和库存
2:修改产品价格
3:增加库存
4:购买指定数量产品
5:增加新产品 作为思考题
0:退出当前系统
'''
def main():
while True:
global price, stock
a = input(f'Please enter a number:{tip}\n').strip()
if a == '0':
print('Exit!')
break
elif a == '1':
style = '{:15}{:6}{:5}'
print(style.format('Name', 'price', 'stock'))
for (n, p), (_, s) in zip(price.items(), stock.items()):
print(style.format(n, p, s))
print()
elif a == '2':
while True:
n = input('enter a product name to modify its price: ')
if n in price:
break
print('invalid input! Should be "{}".'.format(
'" or "'.join(price)))
p = input('enter a new price of this product: ')
price[n] = p
elif a == '3':
while True:
n = input('enter a product name to increase its stock: ')
if n in stock:
break
print('Invalid input! Should be "{}".'.format(
'" or "'.join(stock)))
while True:
s = input('enter a integer to update the stock of it: ')
try:
s = int(s)
break
except:
print('Invalid input, must be a integer!')
stock[n] = str(int(stock[n]) + s)
elif a == '4':
while True:
n = input('enter a product name to buy it: ')
if n in stock:
break
print('Invalid input! Should be "{}".'.format(
'" or "'.join(stock)))
while True:
s = input('enter a integer for how many to buy: ')
try:
s = int(s)
if s <=0 or s > int(stock[n]):
raise
break
except:
print('Invalid input, must be a positive integer and '
'less than{}!'.format(stock[n]))
y = input('You want to buy {} X {}, which cost {}? (y)/n '.format(
n, s, int(price[n]) * s))
if y.strip().lower() in ('y', ''):
stock[n] = str(int(stock[n]) - s)
print('You pay {} and get {} {}'.format(int(price[n]*s), s, n))
elif a == '5':
print('Uncomplete...\n')
if __name__ == '__main__':
main()