Python實現(xiàn)簡單購物車小程序
更新時間:2022年02月09日 08:08:52 作者:村雨遙
這篇文章主要為大家詳細介紹了Python實現(xiàn)簡單購物車小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python實現(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é)果



以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python selenium模塊實現(xiàn)定位過程解析
這篇文章主要介紹了python selenium模塊實現(xiàn)定位過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
放棄 Python 轉(zhuǎn)向 Go語言有人給出了 9 大理由
今年 Stream 團隊的主要編程語言從 Python 轉(zhuǎn)向了 Go。本文解釋了其背后的九大原因以及如何做好這一轉(zhuǎn)換。下面小編給大家分享放棄 Python 轉(zhuǎn)向 Go語言有人給出了 9 大理由,一起看看吧2017-10-10
用python實現(xiàn)操縱mysql數(shù)據(jù)庫插入
大家好,本篇文章主要講的是用python實現(xiàn)操縱mysql數(shù)據(jù)庫插入,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析
這篇文章主要介紹了Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Python獲取list中指定元素索引的兩種方法小結(jié)
本文主要介紹了兩種在Python中獲取列表中指定元素索引的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
淺述python中argsort()函數(shù)的實例用法
本篇文章主要介紹了淺述python中argsort()函數(shù)的實例用法,詳細的介紹了argsort()函數(shù)的用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03

