收入啤酒88 发表于 2018-8-12 09:44:10

Python列表练习

#_*_ coding:utf-8 _*_  
import sys
  
shopping_car = []
  
product_list_title = 'Product list'
  
welcome = 'Welcome to the shopping'
  
product_list = [
  
    ('iphone',3888),
  
    ('thinkpad',4888),
  
    ('coffee',18),
  
    ('mac',6888)
  
]
  
print welcome
  
salary = input('Please input your salary:')
  
while True:
  
    print product_list_title
  
    for item in product_list:
  
      print product_list.index(item)+1,item
  
    choice = input('Please input the name of goods:')
  
    if choice > 4 or choice < 0:
  
      print 'no such goods,please reselect'
  
      continue
  
    elif choice <= 4 and choice >= 1:
  
      if salary < product_list:
  
            print 'Account balance is insufficient, please buy other products or quit'
  
            continue
  
      else:
  
            shopping_car.append(product_list)#将商品添加到购物车
  
            print 'The goods you had buy'
  
            for goods in shopping_car:
  
                print goods,goods
  
            salary = salary - product_list
  
            print 'Your account balance is %s' %salary
  
    elif choice == 0:
  
      print 'Your goods is '
  
      for goods in shopping_car:
  
            print goods, goods
  
      print 'Your balance is',salary
  
      sys.exit('Program exit!!!')
页: [1]
查看完整版本: Python列表练习