第一个简单的python程序,模拟购物车

第一个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:

转载于:https://www.cnblogs.com/qiyuanchang/p/7353271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值