Python_Example_商品结算程序
Posted caochucheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python_Example_商品结算程序相关的知识,希望对你有一定的参考价值。
2018-09-09
IDE: Pycharm2018.02 Python 3.7
KeyWord : List python 基本语句
Explain:
购物车程序
1运行程序,输入预付款,
2允许user根据商品编号购买商品
3user选择商品后,检查余额是否足够,足够直接付款,不够提醒user
4可随时退出,退出时,打印已经购买的商品和余额 ‘‘‘
1------------------------------------------------------------------------------------------------------------------
--
code:
--
import sys ‘‘‘ __author__ = "chu ge " 购物车程序 1运行程序,输入预付款, 2允许user根据商品编号购买商品 3user选择商品后,检查余额是否足够,足够直接付款,不够提醒user 4可随时退出,退出时,打印已经购买的商品和余额 ‘‘‘ product_list =[(‘Book‘,17.99), (‘Phone‘,4999), (‘Cothes‘,298), (‘Apple‘,32.76), (‘Break‘,98), (‘Milk‘,55), (‘Grape‘,25), (‘Wine‘,198), (‘Tea‘,76.89), (‘Biscits‘,49) ] # 提示 print(‘------------------------------------------------‘) print(‘Input Your Payment ‘) payment =input(">>> ") print(‘Payment: %s 元‘ %(payment)) # 加入购物车的空列表 shopping_list = [] if not payment.isdigit(): # 判断是否是数字,如果不是直接退出 print(‘请输入正取的金额...‘) exit() else: # 如果是数字,继续执行 payment = int(payment) while True: # for item in product_list: # print(product_list.index(item),item) # product_list.index(item) 获取下标 # 打印商品信息 for index,item in enumerate(product_list) : # enumerate 把列表下标取出来 print(index,item) # product_list.index(item) 获取下标 user_choice = input("请选择要购买的商品? >>>:") # 判断输入是否为数字 if user_choice.isdigit(): user_choice = int(user_choice) #判断输入长度(范围) if user_choice< len(product_list) and 0 <= user_choice: p_item = product_list[user_choice] if p_item[1] <= payment: # 足够付款 添加购物车 shopping_list.append(p_item) payment -= p_item[1] print("商品信息: 33[32;1m %s 33[0m 付款余额: 33[31;1m %s 33[0m 元" %(p_item,payment)) else: print("足额不足: 33[35;1m %s 33[0m 元" %(payment)) else: print(‘Product Not List ...‘) elif user_choice == ‘q‘: # quit print(" Shopping List....") for q in shopping_list: print(q) print(‘Your Payment: %s ‘%payment) exit() # break
--
Run Result :
--
------------------------------------------------ Input Your Payment >>> 10000 Payment: 10000 元 0 (‘Book‘, 17.99) 1 (‘Phone‘, 4999) 2 (‘Cothes‘, 298) 3 (‘Apple‘, 32.76) 4 (‘Break‘, 98) 5 (‘Milk‘, 55) 6 (‘Grape‘, 25) 7 (‘Wine‘, 198) 8 (‘Tea‘, 76.89) 9 (‘Biscits‘, 49) 请选择要购买的商品? >>>:1 商品信息: (‘Phone‘, 4999) 付款余额: 5001 元 0 (‘Book‘, 17.99) 1 (‘Phone‘, 4999) 2 (‘Cothes‘, 298) 3 (‘Apple‘, 32.76) 4 (‘Break‘, 98) 5 (‘Milk‘, 55) 6 (‘Grape‘, 25) 7 (‘Wine‘, 198) 8 (‘Tea‘, 76.89) 9 (‘Biscits‘, 49) 请选择要购买的商品? >>>:q Shopping List.... (‘Phone‘, 4999) Your Payment: 5001 Process finished with exit code 0
--
以上是关于Python_Example_商品结算程序的主要内容,如果未能解决你的问题,请参考以下文章