
#!/usr/bin/env python3 # py 36+
"""
#要求做一个系统菜单,输入数字进入对应菜单,包含以下内容,正常 *** 作不能报错:
# 菜单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(styleformat('Name', 'price', 'stock'))
for (n, p), (_, s) in zip(priceitems(), stockitems()):
print(styleformat(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 ystrip()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()
1使用IDLE运行python程序
IDLE全称“Integrated Development and Learning Environment”,是python的集成开发和学习环境。它被打包为python包装的可选部分,当安装好python以后,IDLE就自动安装好了,不需要另外去安装。使用IDLE运行python程序方法如下:
2实例:用IDLE编写第一个python程序
(1)运行IDLE开发环境。
代码如下:
price = input("请输入商品价格以空格分割:")strip()
price = list(map(int,pricesplit(' ')))
amount = len(price)
if amount >= 5:
print(f"购买了{amount}件商品,享受八折优惠。优惠后的价格为:{sum(price)08},优惠金额为:{sum(price)02}")
else:
print(f"购买了{amount}件商品。总价为:{sum(price)}")
输出如下:
以上就是关于python文本菜单的程序全部的内容,包括:python文本菜单的程序、python-第二课-用IDLE编写程序、用python编写程序,对顾客购买的商品,让数量大于等于5件时,打八折,求总价等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)