码迷,mamicode.com
首页 > 其他好文 > 详细

购物车小程序

时间:2017-04-22 12:52:55      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:...   允许   提醒   dig   enum   UI   lex   purchase   enumerate   

需求:

1.启动程序后,让用户输入工资,然后打印商品列表

2.允许用户根据商品编号购买商品

3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4.可随时退出,退出时,打印已购买商品和余额

 

salary = input("Input your salary:")

if salary.isdigit():

  salary = int(salary)

else:

  exit("Invaild data type ... ")

 

welcome_msg = ‘Welcome to Alex Shopping mall‘.center(50,‘-‘)

print(welcome_msg)

 

exit_flag = False

product_list = [

  (‘Iphone‘,5888),

  (‘Mac Air‘,8888),

  (‘Mac Pro‘,9888),

  (‘XiaoMi 2‘,19.8),

  (‘Coffee‘,58),

  (‘Tesla‘,880000),

  (‘Bike‘,588),

  (‘Cloth‘,288)]

 

shop_car = []

while exit_flag is not True:

  print("product list".center(50,‘-‘))

  for item in enumerate(product_list):

    index = item[0]

    p_name = item[1][0]

    p_price  = item[1][1]

    print(index,‘.‘,p_name,p_price)

  

  user_choice = input("[q=quit,c=check] What do you want to buy?:")

  if user_choice.isdigit():

    user_choice = int(user_choice)

    if user_choice = int(user_choice)

      p_item = product_list[user_choice]

      if p_item[1] <= salary:

        shop_car.append(p_item)

        salary -= p_item[1]

        print("Added [%s] into shop car, you current balance is [%s]" %(p_item,salary))

      else:

        print("Your banlance is [%s], cannot afford this ..." %salary)

  else: 

    if user_choice == ‘q‘ or user_choice == ‘quit‘:

      print("purchased products as below".center(40,‘*‘))

      for item in shop_car:

        print(item)

      print("End".center(40,‘*‘))

      print("Your balance is [%s]" %salary)

      print("Bye")

      exit_flag = True

    elif user_choice == ‘c‘ or user_choice == ‘check‘    :

      print("purchased products as below".center(40,‘*‘))

      for item in shop_car:

        print(item)

      print("End ... ".center(40,‘*‘))

      print("Your banlance is [%s]" %salary)

 

购物车小程序

标签:...   允许   提醒   dig   enum   UI   lex   purchase   enumerate   

原文地址:http://www.cnblogs.com/stone999/p/6747056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!