python购物车程序-Python编写购物车程序

本人不善言辞,不废话,直接上代码:#!/usr/bin/env python

# -*- coding: utf-8 -*-

#_auth by kk

def goods_list():

shangpin={"iphone":"6000","MacAir":"15000","tea":"50","book":"30"}

print "-"*12 +u"商品列表" +"-"*12

for key in shangpin:

print "goods: %s --> price: %s" % (key,shangpin[key])

print "-"*12 +u"商品列表" +"-"*12

return shangpin

def jiesuan(money,shangpin_list):

sheng_money=money

while True:

qt=raw_input(u"您需要购物吗?y/n ")

if qt=="y":

gou_goods=raw_input(u"请输入您想购买的商品名字,购买的商品请参考商品列表: ")

if gou_goods in shangpin_list:

sheng_money=int(sheng_money)-int(shangpin_list[gou_goods])

if sheng_money>=0:

print " 您购买了%s商品,共消费 %s, 您还剩 %s元钱!" % (gou_goods,shangpin_list[gou_goods],sheng_money)

else:

print "您的金钱不足,请充值,欢迎下次光临!"

break

else:

print "没有此商品!"

else:

print "欢迎下次光临!"

break

if __name__== "__main__":

shangpin_list=goods_list()

try:

money=raw_input(u"请充值后在购买商品,请输入充值金额: ")

print "您的充值金额为 %s,充值成功!"% (money)

except IOError,e:

print "输入错误,请检查!"

jiesuan(money,shangpin_list)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Python 购物车程序代码: ```python # 商品列表 products = { "apple": {"price": 5, "stock": 10}, "banana": {"price": 3, "stock": 5}, "orange": {"price": 4, "stock": 8}, "pear": {"price": 6, "stock": 6} } # 购物车 cart = {} # 打印商品列表 print("商品列表:") for name, attrs in products.items(): print(name, attrs["price"], attrs["stock"]) # 选择商品 while True: name = input("请输入要购买的商品名称:") if name in products: break else: print("商品不存在,请重新输入。") # 选择数量 while True: try: quantity = int(input("请输入要购买的数量:")) if quantity <= products[name]["stock"]: break else: print("库存不足,请重新输入。") except ValueError: print("输入无效,请输入数字。") # 添加到购物车 if name in cart: cart[name]["quantity"] += quantity else: cart[name] = {"price": products[name]["price"], "quantity": quantity} # 更新库存 products[name]["stock"] -= quantity # 打印购物车 print("购物车:") total_price = 0 for name, attrs in cart.items(): price = attrs["price"] * attrs["quantity"] total_price += price print(name, attrs["quantity"], price) # 打印总价 print("总价:", total_price) ``` 该程序实现了以下功能: 1. 打印商品列表。 2. 选择要购买的商品和数量。 3. 添加商品到购物车。 4. 更新商品库存。 5. 打印购物车和总价。 注意:该程序是一个简单的示例,实际应用中需要考虑更多因素,如商品分类、优惠活动、付款方式等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值