Python基础小笔记《2017-10-03》

来源:互联网 发布:网络营销 知乎 编辑:程序博客网 时间:2024/05/14 09:16

一、购物车小程序

product_list = [      ('iphone',5800),      ('Mac book',9800),      ('Bike',800),      ('iWatch',10600),      ]shopping_list = []salary = input("input your salary:")if salary.isdigit():      salary = int(salary)      while True:                for item in product_list:                          print(product_list.index(item),item)                user_choice = input("选择要买的序号:")                if user_choice.isdigit():                         user_choice = int(user_choice)                         if user_choice < len(product_list) and user_choice >= 0:                                  p_item = product_list[user_choice]                                  if p_item[1] <= salary:  #买得起                                          shopping_list.append(p_item)                                          salary -= p_item[1]                                          print('Added %s into shopping cart, your current balance is %s' %(p_item,salary))                                  else:                                          print("Your salary is not enough!!!")                elif user_choice == 'q' or user_choice == 'Q':                          print('Your shopping list:')                          for p in shopping_list:                                 print(p)                          print('Your current balance: ', salary)                          break                else:                          print('invalid option')
原创粉丝点击