Python三落到实处购物车功效,购物车功效的达成

作者: 韦德国际1946手机版  发布:2019-06-03

正文实例讲述了Python实现的购物车功效。分享给大家供大家仿照效法,具体如下:

本文实例为我们分享了Python3实现购物车功用的现实性代码,供大家参照他事他说加以考查,具体内容如下

购物车功能,购物车作用的兑现

js部分

/*加入购物车*/
function loadCar(){
var carData = JSON.parse(getCar());
if(carData){
var carUl = document.getElementById("carUl");
for(var i=0;i<carData.length;i ){
/*增加li*/
var li = document.createElement("li");
li.setAttribute("data-id",carData[i].id)

carUl.appendChild(li);
/*list_left*/
/*增加div*/
var divLeft = document.createElement("div");
divLeft.setAttribute("class","list_left")
li.appendChild(divLeft);
/*复选框*/
var inputDiv = document.createElement("div");
inputDiv.setAttribute("id",("input_") carData[i].id);
inputDiv.setAttribute("onclick","checkboxImg(this)");
var inputImg = document.createElement("img");
inputImg.setAttribute("alt","0");
inputImg.src="./images/checkbox0.png";
inputDiv.appendChild(inputImg);
divLeft.appendChild(inputDiv);
/*产品图片*/
var img = document.createElement("img");
img.setAttribute("onclick","clickImg(this)");
img.src=carData[i].imgSrc;
divLeft.appendChild(img);
Python三落到实处购物车功效,购物车功效的达成。/*产品名称*/
var h6 = document.createElement("h6");
var productTitle=document.createTextNode(carData[i].product_title);
h6.appendChild(productTitle);
divLeft.appendChild(h6);
/*产品介绍*/
var strong = document.createElement("strong");
var strongText =document.createTextNode(carData[i].productExplain)
strong.appendChild(strongText);
divLeft.appendChild(strong);
/*增加p1*/
var p1 = document.createElement("p");
var p1Title=document.createTextNode("功效:")
p1.appendChild(p1Title);
divLeft.appendChild(p1);
/*出品效果*/
var span1=document.createElement("span");
var spanText=document.createTextNode(carData[i].productEffect)
span1.appendChild(spanText)
p1.appendChild(span1);
/*出品颜色*/
var p2 = document.createElement("p");
var p2Title=document.createTextNode("颜色:")
p2.appendChild(p2Title);
divLeft.appendChild(p2);
/*产品颜色*/
var span2=document.createElement("span");
var span2Text=document.createTextNode(carData[i].productColor)
span2.appendChild(span2Text)
p2.appendChild(span2);
/*div:lest_right*/
/*增加div*/
var divRight=document.createElement("div");
divRight.setAttribute("class","list_right");
li.appendChild(divRight);
/*扩展i 商品单价*/
var tageI=document.createElement("i");
var iTitle=document.createTextNode("¥")
tageI.appendChild(iTitle);
var pro_price = document.createTextNode(carData[i].price.toFixed(2))
tageI.appendChild(pro_price);
divRight.appendChild(tageI);
/*增加div*/
var tageDiv=document.createElement("div");
tageDiv.setAttribute("class","pro_number_btn");
divRight.appendChild(tageDiv);
/*减减开关*/
var buttonAdd=document.createElement("button");
var buttonText = document.createTextNode("-");
buttonAdd.setAttribute("onclick","reduce(this)");
buttonAdd.appendChild(buttonText);
tageDiv.appendChild(buttonAdd);
/*数据提醒*/
var inputInfo=document.createElement("input");
inputInfo.setAttribute("type","text");
inputInfo.setAttribute("onchange","numChange(this)");
inputInfo.setAttribute("value",carData[i].num);
tageDiv.appendChild(inputInfo);
/*充实开关*/
var buttonPre=document.createElement("button");
var buttonText2 = document.createTextNode(" ");
buttonPre.setAttribute("onclick","add(this)");
buttonPre.appendChild(buttonText2);
tageDiv.appendChild(buttonPre);
/*追加b 商品小计*/
var tageb=document.createElement("b");
tageb.setAttribute("data-id",carData[i].id)
tageb.setAttribute("id","totalPrice");
var iTitle1=document.createTextNode("¥")
tageb.appendChild(iTitle1);
var total = document.createTextNode(carData[i].totalPrice);
tageb.appendChild(total);
divRight.appendChild(tageb);
/*增加div:cart_btn*/
var tageDivBtn=document.createElement("div");
tageDivBtn.setAttribute("class","cart_btn");
divRight.appendChild(tageDivBtn);
/*产品删除*/
var buttonDel=document.createElement("button");
var buttonDelText = document.createTextNode("删除");
buttonDel.setAttribute("onclick","deletePro(this)")
buttonDel.appendChild(buttonDelText);
tageDivBtn.appendChild(buttonDel);
/*出品收藏按键*/
var buttonGood=document.createElement("button");
var buttonGoodText = document.createTextNode("移动到本身的储藏");
buttonGood.appendChild(buttonGoodText);
tageDivBtn.appendChild(buttonGood);

/*追加清除浮动的div*/
var divClear=document.createElement("div");
divClear.setAttribute("class","clear");
li.appendChild(divClear);
}
}
}
//删除相应的货品
function deletePro(obj){
var li = obj.parentNode.parentNode.parentNode;
var id = li.getAttribute("data-id");
delProduct(id);
li.remove();
}
//商品数量加操作
function add(obj){
var prd_num = obj.previousSibling;
var id = obj.parentNode.parentNode.parentNode.getAttribute("data-id");
var bNode = obj.parentNode.nextSibling;
var num = prd_num.value;
if(isNaN(num)){
num = 1;
}else{
num = parseInt(num);
}

num =1;
prd_num.value = num;
changeCarNum(id,num,bNode);
}
//商品数量改动的操作
function numChange(obj){
var num = obj.value;
var id = obj.parentNode.parentNode.parentNode.getAttribute("data-id");
var bNode = obj.parentNode.nextSibling;
console.log(id)

if(isNaN(num)){
num = 1;
}
num = parseInt(num);
obj.value = num;
changeCarNum(id,num,bNode)
console.log(num)

}
//商品数量减操作
function reduce(obj){
var prd_num = obj.nextSibling;
var id = obj.parentNode.parentNode.parentNode.getAttribute("data-id");
var bNode = obj.parentNode.nextSibling;
var num = prd_num.value;
if(isNaN(num)){
num = 1;
}else{
num = parseInt(num);
}
num-=1;
if(num<1){
num=1;
}
prd_num.value = num;
changeCarNum(id,num,bNode);
}
//更改本地数据的数目
function changeCarNum(id,num,bNode){
var carData = JSON.parse(getCar());
console.log(bNode);
for(var i=0;i<carData.length;i ){
if(carData[i].id == id){
carData[i].num = num;
carData[i].totalPrice = (carData[i].price*num).toFixed(2);
bNode.innerText=("¥" carData[i].totalPrice)
console.log(bNode.innerText)
console.log(carData[i].totalPrice)
break;
}
}
addCar(carData);
}
/*选择打钩*/
var checkboxImg = function (clickObj){
var carData = JSON.parse(getCar());
if(carData){
var img = clickObj.firstChild;
var imgNum = img.alt;
show(imgNum,img)
}
}
/*判定img的alt值 imgNum为图片的alt值,img为图片对象*/
function show(imgNum,img){
if(imgNum === "1"){
img.src="./images/checkbox0.png";
img.setAttribute("alt","0");
}else{
img.src="./images/checkbox.png";
img.setAttribute("alt","1");
}
}
/*点击图片复选框选中*/
function clickImg(imgObj){
var carData = JSON.parse(getCar());
var id = imgObj.parentNode.parentNode.getAttribute("data-id");
if(carData){
var inputImg = imgObj.previousSibling.firstChild
var imgNum = inputImg.alt;
show(imgNum,inputImg)
}
}

上边是去除几点的不

 

上边是地面存款和储蓄

var keyName = "shopCar"
//将货色增加到购物车
function addShopCar(product){
var productData = getCar();
if(!productData){
var proData = [
product
]
addCar(proData)
}else{
var carData = JSON.parse(productData);
var bool = true;
for(var i=0;i<carData.length;i ){
if(carData[i].id == product.id){
carData[i].num=parseInt(carData[i].num) parseInt(product.num);
carData[i].totalPrice = (parseFloat(carData[i].totalPrice) parseFloat(product.totalPrice)).toFixed(2);
bool = false;
break;
}
}
if(bool){
carData.push(product)
}
addCar(carData);
}
}
function getCar(){
return localStorage.getItem(keyName);
}
function addCar(productData){
localStorage.setItem(keyName,JSON.stringify(productData));
}
//通过点名的id删除相应的货色
function delProduct(id){
var carData = JSON.parse(getCar());
var arrData = [];
for(var i=0;i<carData.length;i ){
if(carData[i].id == id){
continue;
}else{
arrData.push(carData[i])
}
}
addCar(arrData);
}
//清空购物车
function clearCar(){
localStorage.removeItem(keyName);
}

function clearProAll(){
clearCar();
var ul = document.getElementById("carUl");
console.log(ul)
if(ul){
var li = ul.getElementsByTagName("li");
var length = li.length;
for(var i=0;i<length;i ){
ul.remove(i);
}
}
}

 

 

html部分

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车大旨</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/page_common.css" />
<link rel="stylesheet" href="css/shop_cart.css" />
<script type="text/javascript" src="js/jquery-1.11.0.js" ></script>
<script type="text/javascript" src="js/bootstrap.js" ></script>
<script src="js/shopCar.js"></script>

<style>
body{width:1200px;margin:0 auto;}
</style>

</head>
<body onload="loadCar()">
<div><a href="javascript:clearProAll();">清空购物车</a></div>
<div class="shop_cart_head">
<h4>全体货物</h肆>
<div class="right">
配送:<select name="" id="">
<option value="">江门市金平区</option>
<option value="">清远市龙门县</option>
<option value="">佛山市德庆县</option>
<option value="">汕头市陆丰市</option>
<option value="">清远市四会市</option>
</select>
</div><!-- right -->
</div><!-- shop_cart_head -->
<div class="shop_cart clear">
<div class="shop_cart_top">
<div><img src="./images/checkbox.png" alt="1" id="checkbox_All" onclick="checkboxAll()"/></div>
<h5>全选</h5>

<h6>商品新闻</h陆>
<ol>
<li>单价</li>
<li>数量</li>
<li>金额</li>
<li>操作</li>
</ol>

</div><!-- shop_cart_top -->
<div class="shop_cart_list">
<ul id="carUl">
<!--<li>
<div class="list_left">

<div id="input" onclick="checkboxImg(this)"><img src="./images/checkbox.png" alt="1" id="aaa"/></div>
<img src="./images/shop_cart_01.jpg" alt="shop_cart_01">
<h陆>雅诗兰黛唇膏口红 女 花漾唇膏炫慕体系 </h陆>
<strong>正品 滋润保湿悠久</strong>
<p>
功效:<span>易上色 滋润 保湿</span>
</p>
<p>
颜色:<span>玫瑰红</span>
</p>
</div>
<div class="list_right">
<i>¥299.00</i>
<div class="pro_number_btn">
<button>-</button>
<input type="text" value="1">
<button> </button>
</div>
<b>¥299.00</b>
<div class="cart_btn">
<button>删除</button>
<button>移动到本身的馆内藏品</button>
</div>
</div>
<div class="clear"></div>
</li>-->
</ul>
</div><!-- shop_cart_list -->
</div><!-- shop_cart -->
<div class="amount">

 

<script>

</script>
<div class="left">
<div><img src="./images/checkbox.png" alt="1" id="aaa" onclick="checkboxImgfx(this)"/></div>
<i>反选</i>
<a href="#">删除选中产品</a>
<a href="#">移动到本身的关切</a>
<a href="#">分享</a>
</div><!-- left -->
<div class="right">
<p>以选中 <span>0</span> 个产品</p>
<p>总价:<i>¥0.00</i></p>
<button>结账</button>
</div><!-- right -->
<div class="clear"></div>
</div><!-- amount -->
<div onclick="checkboxAll()">rtgr</div>

</body>
</html>

 css

 

 

.shop_cart_head>h4{font-size:14px;float:left;margin-top: 20px;}
.shop_cart_head>.right{float:right;margin-top: 20px;margin-bottom: 20px;}
.shop_cart_head>.right>select{border:1px solid #ccc;background-color:#fff;}
.shop_cart{border:1px solid #ccc;}
.shop_cart>.shop_cart_top{background-color:#e2e2e2;padding:10px 20px 4px 20px;border-bottom: 1px solid #ccc;height:60px;}

.shop_cart>.shop_cart_top>div{float:left;height:13px;width:13px;border:1px solid #ccc;position: relative;margin-top:12px;}
.shop_cart>.shop_cart_top>div>img{position: absolute;left:-2px;top:-4px;}
.shop_cart>.shop_cart_top>h5{float:left;margin-left: 10px;margin-top:12px;}
.shop_cart>.shop_cart_top>h6{float:left;margin-left: 156px;margin-top:12px;}
.shop_cart>.shop_cart_top>ol{float:right;margin-right:60px;margin-top: 10px;}
.shop_cart>.shop_cart_top>ol>li{display: inline-block;margin-left: 90px;}
/* 购物车列表 */
/* 左部分 */
.shop_cart_list>ul{padding:0px 20px;margin-bottom:0px;}
.shop_cart_list>ul>li{border-bottom:1px solid #ccc;}
.shop_cart_list>ul>li:last-child{border-bottom:0px;}
.shop_cart_list>ul>li>.list_left{float:left;}
.shop_cart_list>ul>li>.list_left>div{float:left;margin-top: 72px;height:13px;width:13px;border:1px solid #ccc;position: relative;}
.shop_cart_list>ul>li>.list_left>div>img{position: absolute;left:-2px;top:-4px;}
.shop_cart_list>ul>li>.list_left>img{float:left;width:120px;height: 120px;margin:20px 10px;}
.shop_cart_list>ul>li>.list_left>h6{display: inline-block;color:#666;margin-top: 30px;margin-bottom: 6px;}
.shop_cart_list>ul>li>.list_left>strong{color:#666;display:block;}
.shop_cart_list>ul>li>.list_left>p{color:#999;margin-top: 6px;margin-bottom: 4px;}
/* 右部分 */
.shop_cart_list>ul>li>.list_right{float:right;padding:60px 20px 40px 0px;}
.shop_cart_list>ul>li>.list_right>i{font-style: normal;margin-right:64px;float:left;display:block;margin-top:22px;}
.shop_cart_list>ul>li>.list_right>.pro_number_btn{margin-right:47px;display: inline-block;border:1px solid #d3d3d3;color:#999;float:left;display:block;margin-top:19px;}
.shop_cart_list>ul>li>.list_right>.pro_number_btn>button{background-color:#f0f0f0;width:20px;height:20px;border-left:1px solid #d3d3d3;}
.shop_cart_list>ul>li>.list_right>.pro_number_btn>button:first-child{background-color: #e2e2e2;border-left:0px;border-right:1px solid #d3d3d3;}
.shop_cart_list>ul>li>.list_right>.pro_number_btn>input{text-align: center;background-color:#f0f0f0;width:40px;border:0px;}
.shop_cart_list>ul>li>.list_right>b{font-weight: normal;margin-right:50px;float:left;display:block;width:70px;text-align:center;margin-top:20px;}
.shop_cart_list>ul>li>.list_right>.cart_btn{display: inline-block;float:left;display:block;margin-top:6px;}
.shop_cart_list>ul>li>.list_right>.cart_btn>button{display:block;background-color:#f0f0f0;margin-bottom: 10px;}
/* 买下账单部分 */
.amount{color:#666;background-color:#dedede;margin-top: 20px;margin-bottom: 30px;padding-left:20px;border:1px solid #ccc;}
.amount>.left{float:left;margin-top: 14px;}
.amount>.left>div{float:left;height:13px;width:13px;border:1px solid #ccc;position: relative;margin-top:4px;}
.amount>.left>div>img{position: absolute;left:-2px;top:-4px;}
.amount>.left>i{display:block;float:left;font-style: normal;font-size: 14px;margin-left: 10px;}
.amount>.left>a{display:block;float:left;color:#666;font-size: 14px;margin-left:20px;}
.amount>.right{float:right;}
.amount>.right>p{display: inline-block;font-size: 14px;margin-right: 20px;}
.amount>.right>p>i{font-style:normal;color:#cc0000;font-size: 18px;}
.amount>.right>button{background-color: #cc0000;font-size: 18px;height:50px;line-height:50px;padding:0px 28px;color:#fff;}
.page_btn{padding-bottom:30px;}

/* 搜索框 */
header .header_cont .input_search{margin-left:456px;}

js部分 /*进入购物车*/ function loadCar(){ var carData = JSON.parse(getCar()); if(carData){ var carUl = document.getElementById("ca...

Python完成购物车功用的艺术分析,python购物车

正文实例讲述了Python落成购物车功用的法子。分享给大家供大家参谋,具体如下:

一、程序的源代码如下:

salary = input('input your salary:')
if salary.isdigit:
  salary = int(salary)
else:
  exit('salary is not digit!!')
welcome_msg = 'welcome to our shoping mall'
print(welcome_msg.center(50,'-'))
product_list = [
  ('Iphone',5888),
  ('Mac Air',8000),
  ('XiaoMi',19.9),
  ('coffee',30),
  ('Tesla',820000),
  ('Bike',700),
  ('Cloth',200)
]
shop_car = []
#推出标志位
exit_flag = 0
while exit_flag is not True:
  print('product list :'.center(50,'-'))
  for item in enumerate(product_list):
    index = item[0]   #获得商品序号
    p_name = item[1][0]  #获得商品名称
    p_price= item[1][1]  #获得商品价格
    print(index,p_name,p_price)
  user_choice = input('[q=quit,c=check] what do you want to buy?:')
  if user_choice.isdigit():
    user_choice = int(user_choice)
    if user_choice < len(product_list): #输入的商品序号要在范围之内
      p_item = product_list[user_choice] #选择的商品
      if p_item[1] <= salary: #买得起
        shop_car.append(p_item) #放入购物车
        salary -= p_item[1] #减钱
        print('purchased products are:'.center(40, '-'))
        for item in shop_car:
          print(item)
        print('Your balance is [%s]' % salary)
      else:          #买不起
        print('Your balance is [%s],cannot buy the product!'%salary)
    else:
      print('we do not have the product,please input again!')
  elif user_choice == 'q' or user_choice == 'quit':
    print('purchased products are:'.center(40,'-'))
    for item in shop_car:
      print(item)
    print('END '.center(40,'-'))
    print('Your balance is [%s]'%salary)
    exit_flag =True
  elif user_choice == 'c' or user_choice == 'check':
    print('purchased products are:'.center(40, '-'))
    for item in shop_car:
      print(item)
    print('Your balance is %d' % salary)
    print('Check '.center(40, '-'))
  else:
    print('Incorrect input ,please input again!!')

二、程序运转结果

input your salary:10000
-----------welcome to our shoping mall------------
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:0
added [[('Iphone', 5888)]] into shop car ,yourcurrent balence is 4112
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:1
Your balance is [4112],cannot buy the product!
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:c
shop car list are [[('Iphone', 5888)]],your balance is 4112
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:4
Your balance is [4112],cannot buy the product!
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:q
--------purchased products are:---------
('Iphone', 5888)
------------------END ------------------
Your balance is [4112]
Process finished with exit code 0

三、学习到的知识点

(1)print('product list :'.center(50,'-'))

>>> print('product list :'.center(50,'-'))
------------------product list :------------------

(2)for item in enumerate(product_list):

>>> for item in enumerate(product_list):
print(item)
(0, ('Iphone', 5888))
(1, ('Mac Air', 8000))
(2, ('XiaoMi', 19.9))
(3, ('coffee', 30))
(4, ('Tesla', 820000))
(5, ('Bike', 700))
(6, ('Cloth', 200))

enumerate函数重回多少个生成器对象:(index,item)的元组。

(3)这里运用了列表 元组的款式累积商品列表,而不是字典,因为字典是冬辰的,每一回打字与印刷顺序都不雷同,而且无法透过索引实行取值。

更加多关于Python相关内容感兴趣的读者可查阅本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技艺总括》、《Python编码操作才干总计》、《Python函数使用技能计算》、《Python字符串操作技能汇总》及《Python入门与进级美貌教程》

企望本文所述对大家Python程序设计有所帮忙。

本文实例讲述了Python完毕购物车功能的办法。分享给大家供我们参谋,具体如下: 1、程序的...

此地品尝用python完毕简单的购物车程序。。。

购物车必要:

主导供给:

1、运行程序后,输入用户名密码后,假如是第叁次登6,让用户输入薪酬,然后打字与印刷商品列表

用户输入薪金,然后打字与印刷购物菜单
用户可以穿梭的买入商品,直到余额非常不足停止
剥离时打字与印刷用户已购置的物品和剩余金额。。。

2、允许用户依照商品编号购买商品

代码:

三、用户选拔商品后,检查实验余额是还是不是够,够就直接扣款,相当不足就提醒

本文由韦德国际1946发布于韦德国际1946手机版,转载请注明出处:Python三落到实处购物车功效,购物车功效的达成

关键词: