網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Python實(shí)現(xiàn)簡單購物車小程序的具體代碼,供大家參考,具體內(nèi)容如下
要求
代碼
# --*--coding:utf-8--*-- # Author: 村雨 import pprint productList = [('Iphone 8', 10000), ? ? ? ? ? ? ? ?('GTX2080', 8000), ? ? ? ? ? ? ? ?('Z7KP7-GT', 6000), ? ? ? ? ? ? ? ?('Mac pro', 15000), ? ? ? ? ? ? ? ?('Honor 10', 2800), ? ? ? ? ? ? ? ?('Iphone XR', 12000), ? ? ? ? ? ? ? ?('Mi 8', 2999) ? ? ? ? ? ? ? ?] shoppingList = [] print('輸入你的工資:') salary = input() if not salary.isdigit(): ? ? print('請輸入整數(shù)') else: ? ? salary = int(salary) ? ? while True: ? ? ? ? for index, item in enumerate(productList): ? ? ? ? ? ? print(index + 1, item) ? ? ? ? print('輸入你要買的商品的序號:') ? ? ? ? userWant = input() ? ? ? ? if userWant.isdigit(): ? ? ? ? ? ? userWant = int(userWant) ? ? ? ? ? ? if userWant <= len(productList) and userWant > 0: ? ? ? ? ? ? ? ? print('你要購買的是:', productList[userWant - 1][0]) ? ? ? ? ? ? ? ? if salary >= productList[userWant - 1][1]: ? ? ? ? ? ? ? ? ? ? shoppingList.append(productList[userWant - 1][0]) ? ? ? ? ? ? ? ? ? ? salary -= productList[userWant - 1][1] ? ? ? ? ? ? ? ? ? ? print('你已經(jīng)購買了' + productList[userWant - 1][0] + ', 你的余額為 ' + str(salary)) ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? print('對不起,你的余額不足!請努力工作吧!') ? ? ? ? ? ? ? ? ? ? print('你當(dāng)前所購買的商品為:') ? ? ? ? ? ? ? ? ? ? for brought in shoppingList: ? ? ? ? ? ? ? ? ? ? ? ? pprint.pprint(brought) ? ? ? ? ? ? ? ? ? ? print('你當(dāng)前余額為:', salary) ? ? ? ? ? ? ? ? ? ? exit() ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? print('你輸入的商品序號有錯,請重新輸入') ? ? ? ? elif userWant == 'q': ? ? ? ? ? ? print('-----------Shopping List----------') ? ? ? ? ? ? for brought in shoppingList: ? ? ? ? ? ? ? ? pprint.pprint(brought) ? ? ? ? ? ? print('你的余額為 ', salary) ? ? ? ? ? ? exit() ? ? ? ? else: ? ? ? ? ? ? print('Invalid input!??!')
結(jié)果
原文鏈接:https://blog.csdn.net/github_39655029/article/details/82747768
相關(guān)推薦
- 2023-03-18 Python?lambda匿名函數(shù)深入講解_python
- 2022-01-10 Layui碰上Thymeleaf的解析問題
- 2022-12-23 Android?Studio中如何修改APP圖標(biāo)和APP名稱_Android
- 2022-06-07 C語言超詳細(xì)講解排序算法下篇_C 語言
- 2022-11-27 Oracle?中檢查臨時表空間的方法_oracle
- 2022-04-27 jQuery實(shí)現(xiàn)移動端懸浮拖動效果_jquery
- 2022-02-14 Uncaught TypeError: Failed to execute ‘a(chǎn)ppendChild
- 2022-10-22 Golang?中反射的應(yīng)用實(shí)例詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支