第一个pyhone程序,简单的购物车功能,刚开始学习,练习下手感。

#/usr/bin/env python
#-*- coding:utf-8 -*-

salary = int(raw_input("请输入您的工资:").strip())

shop_list=[[1,'iphone6s',5800],[2,'mac book',9000],[3,'coff',32],[4,'python book',80],[5,'bicyle',1500]]
user_list=[]

print('''商品列表如下:
1.iphone6s 5800
2.mac book 9000
3.coff 32
4.pyhone book 80
5.bicyle 1500
''')

while True:
  select = int(raw_input("请选择需要购买的商品编号:").strip())

  if select >= len(shop_list):
    print("商品编号不存在,请重新输入!")
    continue
  else:
    Number = shop_list[select-1][0]
    Product = shop_list[select-1][1]
    Price = shop_list[select-1][2]

  if salary >= Price:
    user_list.append(Product)
    salary = salary - Price
    print("已购买商品:%s") %str(user_list)
    print("已消费%s元,余额为%s元。") %(Price,salary)
    continue
  else:
    Difference = Price - salary
    print("您的工资无法满足此产品价格,还差%s元,请重新选择其他商品!") %Difference
    continue

 

程序执行结果:

root@virtual-machine:/opt/python# python 1.py
请输入您的工资:50000
商品列表如下:
1.iphone6s 5800
2.mac book 9000
3.coff 32
4.pyhone book 80
5.bicyle 1500

请选择需要购买的商品编号,退出请输入exit:1
已购买商品:['iphone6s']
已消费5800元,余额为44200元。
请选择需要购买的商品编号,退出请输入exit:2
已购买商品:['iphone6s', 'mac book']
已消费9000元,余额为35200元。
请选择需要购买的商品编号,退出请输入exit:3
已购买商品:['iphone6s', 'mac book', 'coff']
已消费32元,余额为35168元。
请选择需要购买的商品编号,退出请输入exit:4
已购买商品:['iphone6s', 'mac book', 'coff', 'python book']
已消费80元,余额为35088元。
请选择需要购买的商品编号,退出请输入exit:5
商品编号不存在,请重新输入!
请选择需要购买的商品编号,退出请输入exit:1
已购买商品:['iphone6s', 'mac book', 'coff', 'python book', 'iphone6s']
已消费5800元,余额为29288元。
请选择需要购买的商品编号,退出请输入exit:2
已购买商品:['iphone6s', 'mac book', 'coff', 'python book', 'iphone6s', 'mac book']
已消费9000元,余额为20288元。
请选择需要购买的商品编号,退出请输入exit:3
已购买商品:['iphone6s', 'mac book', 'coff', 'python book', 'iphone6s', 'mac book', 'coff']
已消费32元,余额为20256元。
请选择需要购买的商品编号,退出请输入exit:4
已购买商品:['iphone6s', 'mac book', 'coff', 'python book', 'iphone6s', 'mac book', 'coff', 'python book']
已消费80元,余额为20176元。
请选择需要购买的商品编号,退出请输入exit:6
商品编号不存在,请重新输入!
请选择需要购买的商品编号,退出请输入exit:

相关文章: