最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)購(gòu)物車功能

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

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

功能要求:

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

代碼:

#!/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)單,就是涉及到列表的增加和刪除,還有一些邏輯的判斷處理。

運(yùn)行結(jié)果如下:

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

相關(guān)文章

  • Python訪問PostgreSQL數(shù)據(jù)庫(kù)詳細(xì)操作

    Python訪問PostgreSQL數(shù)據(jù)庫(kù)詳細(xì)操作

    postgresql是常用的關(guān)系型數(shù)據(jù)庫(kù),并且postgresql目前還保持著全部開源的狀態(tài),這篇文章主要給大家介紹了關(guān)于Python訪問PostgreSQL數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • pandas:get_dummies()與pd.factorize()的用法及區(qū)別說(shuō)明

    pandas:get_dummies()與pd.factorize()的用法及區(qū)別說(shuō)明

    這篇文章主要介紹了pandas:get_dummies()與pd.factorize()的用法及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python常用小技巧總結(jié)

    Python常用小技巧總結(jié)

    這篇文章主要介紹了Python常用小技巧,實(shí)例總結(jié)了Python關(guān)于字典、字符串、隨機(jī)數(shù)等操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-06-06
  • Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新

    Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新

    dwebsocket是Python中一款用于實(shí)現(xiàn)WebSocket協(xié)議的庫(kù),可用于后端數(shù)據(jù)實(shí)時(shí)刷新。在Django中結(jié)合使用dwebsocket和Channels,可以實(shí)現(xiàn)前后端的實(shí)時(shí)通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實(shí)時(shí)聊天、數(shù)據(jù)監(jiān)控、在線游戲等場(chǎng)景
    2023-04-04
  • TensorFlow人工智能學(xué)習(xí)Keras高層接口應(yīng)用示例

    TensorFlow人工智能學(xué)習(xí)Keras高層接口應(yīng)用示例

    這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)中Keras高層接口的應(yīng)用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • 2020新版本pycharm+anaconda+opencv+pyqt環(huán)境配置學(xué)習(xí)筆記,親測(cè)可用

    2020新版本pycharm+anaconda+opencv+pyqt環(huán)境配置學(xué)習(xí)筆記,親測(cè)可用

    這篇文章主要介紹了2020新版本pycharm+anaconda+opencv+pyqt環(huán)境配置學(xué)習(xí)筆記,親測(cè)可用,特此分享到腳本之家平臺(tái),需要的朋友可以參考下
    2020-03-03
  • HTTPX入門使用教程

    HTTPX入門使用教程

    HTTPX是一款Python棧HTTP客戶端庫(kù),它提供了比標(biāo)準(zhǔn)庫(kù)更高級(jí)別、更先進(jìn)的功能,如連接重用、連接池、超時(shí)控制、自動(dòng)繁衍請(qǐng)求,下面通過本文介紹HTTPX入門知識(shí)和基本用法,感興趣的朋友一起看看吧
    2023-12-12
  • python線程中的同步問題及解決方法

    python線程中的同步問題及解決方法

    這篇文章主要介紹了python線程中的同步問題及解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python標(biāo)準(zhǔn)庫(kù)OS模塊詳解

    python標(biāo)準(zhǔn)庫(kù)OS模塊詳解

    這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù)OS模塊詳細(xì)介紹,需要的朋友可以參考下
    2020-03-03
  • python多進(jìn)程使用函數(shù)封裝實(shí)例

    python多進(jìn)程使用函數(shù)封裝實(shí)例

    這篇文章主要介紹了python多進(jìn)程使用函數(shù)封裝實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-05-05

最新評(píng)論

宁城县| 修文县| 大名县| 株洲县| 吐鲁番市| 赤水市| 安岳县| 广宗县| 高青县| 安阳市| 喀喇沁旗| 浠水县| 东乡族自治县| 调兵山市| 潜山县| 菏泽市| 衡山县| 鄂尔多斯市| 麟游县| 承德市| 芜湖县| 社旗县| 祁东县| 乡宁县| 贵州省| 凤城市| 中超| 盐池县| 简阳市| 农安县| 炎陵县| 沁源县| 会宁县| 六盘水市| 郁南县| 苗栗市| 安龙县| 岳阳市| 会泽县| 大连市| 兴山县|