Python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車小程序
本文實(shí)例為大家分享了Python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車小程序的具體代碼,供大家參考,具體內(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('輸入你要買的商品的序號(hào):')
? ? ? ? userWant = input()
? ? ? ? if userWant.isdigit():
? ? ? ? ? ? userWant = int(userWant)
? ? ? ? ? ? if userWant <= len(productList) and userWant > 0:
? ? ? ? ? ? ? ? print('你要購(gòu)買的是:', 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)買了' + productList[userWant - 1][0] + ', 你的余額為 ' + str(salary))
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print('對(duì)不起,你的余額不足!請(qǐng)努力工作吧!')
? ? ? ? ? ? ? ? ? ? print('你當(dāng)前所購(gòu)買的商品為:')
? ? ? ? ? ? ? ? ? ? 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é)果



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用ctypes調(diào)用C/C++的方法
今天小編就為大家分享一篇關(guān)于Python使用ctypes調(diào)用C/C++的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
wxpython中Textctrl回車事件無(wú)效的解決方法
這篇文章主要介紹了wxpython中Textctrl回車事件無(wú)效的解決方法,較為詳細(xì)的分析了TextCtrl支持的事件類型,并給出了TextCtrl綁定回車事件的相應(yīng)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
Python?第三方庫(kù)?Pandas?數(shù)據(jù)分析教程
這篇文章主要介紹了Python?第三方庫(kù)?Pandas?數(shù)據(jù)分析教程的相關(guān)資料,需要的朋友可以參考下2022-09-09
python多進(jìn)程實(shí)現(xiàn)進(jìn)程間通信實(shí)例
這篇文章主要介紹了python多進(jìn)程實(shí)現(xiàn)進(jìn)程間通信實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
python中如何實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用
這篇文章主要介紹了python中如何實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
關(guān)于numpy和torch.tensor的張量的操作
這篇文章主要介紹了關(guān)于numpy和torch.tensor的張量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

