小編這次要給大家分享的是Python如何基于數(shù)列實(shí)現(xiàn)購物車功能,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
要求
1、啟動程序后讓用戶輸入余額,并打印商品列表
2、用戶通過輸入編號購買商品
3、用戶選擇商品購買后,根據(jù)余額判斷成功或者失敗,給出對應(yīng)提示
4、可以隨時(shí)退出,退出后打印賬號余額以及購買的商品列表
構(gòu)思
1、首先,用戶余額需要進(jìn)行存儲,用戶購買的物品需要進(jìn)行存儲在數(shù)組中
2、用戶購買成功后,將購買的物品放入物品集合,并用總金額減去余額
3、如果失敗,給出失敗提示,并打印余額
4、用戶選擇繼續(xù)后,無論成功失敗,都可以繼續(xù)購買
代碼
# 用戶輸入工資 balance = int(input("Please input balance:")) # 定義衣服的數(shù)組 clothes = [["pants",100],["T-shirt",50],["skirt",20]] # 個(gè)人所得,包括金錢和獲取的物品 haveGoods = [balance,[]] flag = True while flag: # 打印衣服列表 print("The clothes list is as follows") print("______clothesList______") i = 1; for c in clothes: print('The number:',i,":",c) i += 1 # 用戶輸入商品編號 code = int(input("Please choose the number:")) # 判斷錢是否夠用 if clothes[code-1][1] <= haveGoods[0]: # 在自己的購物清單中加入已購物品 haveGoods[1].append(clothes[code-1]) # 減去花費(fèi)的金錢 haveGoods[0] -= clothes[code-1][1] print("You have successfully purchased!") print("Your account balance is:",haveGoods[0]) else: print("Your account balance is insufficient!") print("Your account balance is:",haveGoods[0]) judge = input("You can press any button to continue,or input 'n' to leave:") if judge == "n": flag = False print("Your account balance is:",haveGoods[0]) print("Your shopping list is as follows:") print("______clothesList______") for h in haveGoods[1]: print(h)
當(dāng)前標(biāo)題:Python如何基于數(shù)列實(shí)現(xiàn)購物車功能-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://chinadenli.net/article8/dhjeip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、軟件開發(fā)、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)站營銷、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容