亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python實現(xiàn)購物車功能

 更新時間:2022年02月09日 09:23:46   作者:亂彈世界  
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)購物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)購物車功能的具體代碼,供大家參考,具體內(nèi)容如下

功能要求:

要求用戶輸入總資產(chǎn),例如:2000
顯示商品列表,讓用戶根據(jù)序號選擇商品,加入購物車
購買,如果商品總額大于總資產(chǎn),提示賬戶余額不足,否則,購買成功。
附加:可充值、某商品移除購物車

代碼:

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

shopping_list = [
? ? ? ? ("Iphone", 5000),
? ? ? ? ("Delicious food", 48),
? ? ? ? ("Mac book", 9800),
? ? ? ? ("Huawei", 4800),
? ? ? ? ("Alex python", 32),
? ? ? ? ("coffee", 24)
]
shopping_cart = []
salary = raw_input('please input salary: ')
if not salary.isdigit():
? ? ? ? print "salary must be digit,run again"
? ? ? ? exit()
else:
? ? ? ? salary = int(salary)

while True:
? ? ? ? print "------products list is--------"
? ? ? ? for index, item in enumerate(shopping_list):
? ? ? ? ? ? ? ? print "\033[32m%s, %s\033[0m" %(index, item)
? ? ? ? choice = raw_input('please input choice[q(uit)]>>> ')
? ? ? ? if choice.isdigit():
? ? ? ? ? ? ? ? choice = int(choice)
? ? ? ? ? ? ? ? if choice < len(shopping_list) and choice >= 0:
? ? ? ? ? ? ? ? ? ? ? ? product = shopping_list[choice]
? ? ? ? ? ? ? ? ? ? ? ? if salary > product[1]:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? confirm = raw_input('do you want to buy now[y/n]: ')
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if confirm == 'y':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shopping_cart.append(product)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? salary -= product[1]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "you bought %s,price is %d, your balance is %d" % (product[0], product[1], salary)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print 'select again'
? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add_confirm = raw_input("your balance is: %d, not enough, do you want to add more?[y/n]" % salary)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if add_confirm == 'y':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add_salary = raw_input('add the money: ')
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if add_salary.isdigit():
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add_salary = int(add_salary)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? salary += add_salary
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "now balance is %d: " % salary
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "the money must be digit."
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "------shopping cart list---------: "
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for index, item in enumerate(shopping_cart):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print index, item
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? print "choice must be 0~5."
? ? ? ? elif choice == 'q':
? ? ? ? ? ? ? ? remove_product = raw_input("do you want remove product or exits now [y/n] ")
? ? ? ? ? ? ? ? if remove_product == "y":
? ? ? ? ? ? ? ? ? ? ? ? print "-----------your shopping cart lists-------------: "
? ? ? ? ? ? ? ? ? ? ? ? for index, item in enumerate(shopping_cart):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print index, item
? ? ? ? ? ? ? ? ? ? ? ? remove_choice = raw_input('please input your remove choice>>> ')
? ? ? ? ? ? ? ? ? ? ? ? if remove_choice.isdigit() and int(remove_choice) < len(shopping_cart) and int(remove_choice) >= 0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? salary += shopping_cart[int(remove_choice)][1]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? del shopping_cart[int(remove_choice)]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "-----------new shopping cart lists-------------: "
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for index, item in enumerate(shopping_cart):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print index, item
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "your balance is %d" % salary
? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print "input error, again"
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? print "exit now"
? ? ? ? ? ? ? ? ? ? ? ? exit()

? ? ? ? else:
? ? ? ? ? ? ? ? print "-----------shopping cart lists-------------: "
? ? ? ? ? ? ? ? for index, item in enumerate(shopping_cart):
? ? ? ? ? ? ? ? ? ? ? ? print index, item
? ? ? ? ? ? ? ? print "\033[31mchoice must be digit,exit\033[0m"

功能挺簡單,就是涉及到列表的增加和刪除,還有一些邏輯的判斷處理。

運行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解python中的Turtle函數(shù)庫

    詳解python中的Turtle函數(shù)庫

    這篇文章主要介紹了python中的Turtle函數(shù)庫,包括函數(shù)庫的引用方式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Python中super函數(shù)用法實例分析

    Python中super函數(shù)用法實例分析

    這篇文章主要介紹了Python中super函數(shù)用法,結(jié)合實例形式詳細(xì)分析了Python中super函數(shù)的功能、調(diào)用父類相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下
    2019-03-03
  • 在Python中用get()方法獲取字典鍵值的教程

    在Python中用get()方法獲取字典鍵值的教程

    這篇文章主要介紹了在Python中用get()方法獲取字典鍵值的教程,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Python字典的基本用法實例分析【創(chuàng)建、增加、獲取、修改、刪除】

    Python字典的基本用法實例分析【創(chuàng)建、增加、獲取、修改、刪除】

    這篇文章主要介紹了Python字典的基本用法,結(jié)合具體實例形式分析了Python字典的創(chuàng)建、增加、獲取、修改、刪除等基本操作技巧與注意事項,需要的朋友可以參考下
    2019-03-03
  • Pyhton自動化測試持續(xù)集成和Jenkins

    Pyhton自動化測試持續(xù)集成和Jenkins

    這篇文章介紹了Pyhton自動化測試持續(xù)集成和Jenkins,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • python讀取當(dāng)前目錄下的CSV文件數(shù)據(jù)

    python讀取當(dāng)前目錄下的CSV文件數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了python讀取當(dāng)前目錄下的CSV文件數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • python爬蟲請求頭設(shè)置代碼

    python爬蟲請求頭設(shè)置代碼

    在本篇文章里小編給大家整理的是一篇關(guān)于python爬蟲請求頭如何設(shè)置內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • Python使用Selenium模擬瀏覽器自動操作功能

    Python使用Selenium模擬瀏覽器自動操作功能

    這篇文章主要介紹了Python使用Selenium模擬瀏覽器自動操作功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Pytorch之如何提取模型中的某一層

    Pytorch之如何提取模型中的某一層

    這篇文章主要介紹了Pytorch之如何提取模型中的某一層問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python的dict判斷key是否存在的方法

    python的dict判斷key是否存在的方法

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python的dict判斷key是否存在的方法,有需要的朋友們可以參考下。
    2020-12-12

最新評論