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('你當前所購買的商品為:') ? ? ? ? ? ? ? ? ? ? for brought in shoppingList: ? ? ? ? ? ? ? ? ? ? ? ? pprint.pprint(brought) ? ? ? ? ? ? ? ? ? ? print('你當前余額為:', salary) ? ? ? ? ? ? ? ? ? ? exit() ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? print('你輸入的商品序號有錯,請重新輸入') ? ? ? ? elif userWant == 'q': ? ? ? ? ? ? print('-----------Shopping List----------') ? ? ? ? ? ? for brought in shoppingList: ? ? ? ? ? ? ? ? pprint.pprint(brought) ? ? ? ? ? ? print('你的余額為 ', salary) ? ? ? ? ? ? exit() ? ? ? ? else: ? ? ? ? ? ? print('Invalid input?。?!')
結果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python使用ctypes調(diào)用C/C++的方法
今天小編就為大家分享一篇關于Python使用ctypes調(diào)用C/C++的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01Python?第三方庫?Pandas?數(shù)據(jù)分析教程
這篇文章主要介紹了Python?第三方庫?Pandas?數(shù)據(jù)分析教程的相關資料,需要的朋友可以參考下2022-09-09