網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了Python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)小程序的具體代碼,供大家參考,具體內(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('請(qǐng)輸入整數(shù)') else: ? ? salary = int(salary) ? ? while True: ? ? ? ? for index, item in enumerate(productList): ? ? ? ? ? ? print(index + 1, item) ? ? ? ? print('輸入你要買(mǎi)的商品的序號(hào):') ? ? ? ? userWant = input() ? ? ? ? if userWant.isdigit(): ? ? ? ? ? ? userWant = int(userWant) ? ? ? ? ? ? if userWant <= len(productList) and userWant > 0: ? ? ? ? ? ? ? ? print('你要購(gòu)買(mǎi)的是:', productList[userWant - 1][0]) ? ? ? ? ? ? ? ? if salary >= productList[userWant - 1][1]: ? ? ? ? ? ? ? ? ? ? shoppingList.append(productList[userWant - 1][0]) ? ? ? ? ? ? ? ? ? ? salary -= productList[userWant - 1][1] ? ? ? ? ? ? ? ? ? ? print('你已經(jīng)購(gòu)買(mǎi)了' + productList[userWant - 1][0] + ', 你的余額為 ' + str(salary)) ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? print('對(duì)不起,你的余額不足!請(qǐng)努力工作吧!') ? ? ? ? ? ? ? ? ? ? print('你當(dāng)前所購(gòu)買(mǎi)的商品為:') ? ? ? ? ? ? ? ? ? ? for brought in shoppingList: ? ? ? ? ? ? ? ? ? ? ? ? pprint.pprint(brought) ? ? ? ? ? ? ? ? ? ? print('你當(dāng)前余額為:', salary) ? ? ? ? ? ? ? ? ? ? exit() ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? print('你輸入的商品序號(hào)有錯(cuò),請(qǐng)重新輸入') ? ? ? ? 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)推薦
- 2022-03-29 jquery實(shí)現(xiàn)頁(yè)面彈球效果_jquery
- 2022-09-15 Nginx如何獲取自定義請(qǐng)求header頭和URL參數(shù)詳解_nginx
- 2022-04-09 iOS浮點(diǎn)類(lèi)型精度問(wèn)題的原因與解決辦法_IOS
- 2022-06-07 淺談Python中對(duì)象是如何被調(diào)用的_python
- 2022-03-31 python猜單詞游戲的實(shí)現(xiàn)_python
- 2023-04-07 關(guān)于vector的常見(jiàn)用法詳解_C 語(yǔ)言
- 2022-10-03 Go?Excelize?API源碼閱讀Close及NewSheet方法示例解析_Golang
- 2023-07-03 python?遍歷可迭代對(duì)象的實(shí)現(xiàn)方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤: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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支