網站首頁 編程語言 正文
本文實例為大家分享了python實現購物車功能的具體代碼,供大家參考,具體內容如下
功能要求:
要求用戶輸入總資產,例如:2000
顯示商品列表,讓用戶根據序號選擇商品,加入購物車
購買,如果商品總額大于總資產,提示賬戶余額不足,否則,購買成功。
附加:可充值、某商品移除購物車
代碼:
#!/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"
功能挺簡單,就是涉及到列表的增加和刪除,還有一些邏輯的判斷處理。
運行結果如下:
原文鏈接:https://blog.csdn.net/linxi7/article/details/70582273
相關推薦
- 2022-09-02 ahooks整體架構及React工具庫源碼解讀_React
- 2022-06-19 python繪制橫向水平柱狀條形圖_python
- 2022-11-07 Vite+React搭建開發構建環境實踐記錄_React
- 2022-11-21 詳解React獲取DOM和獲取組件實例的方式_React
- 2022-09-09 python?對excel交互工具的使用詳情_python
- 2022-12-08 C#?如何調用C++?dll?string類型返回_C#教程
- 2022-10-20 Kotlin作用域函數應用詳細介紹_Android
- 2022-07-03 C#并行編程之信號量_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支