日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)小程序_python

作者:村雨遙 ? 更新時(shí)間: 2022-04-14 編程語(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

欄目分類(lèi)
最近更新