使用python的列表實現購物車程式

2021-08-22 03:29:07 字數 1121 閱讀 4409

# author richard_kong

# !/usr/bin/env python

# --*-- encoding:utf-8 --*--

"""請閉眼寫出以下程式。

程式:購物車程式

需求: 啟動程式後,讓使用者輸入工資,然後列印商品列表

允許使用者根據商品編號購買商品

使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒

可隨時退出,退出時,列印已購買商品和餘額

"""shopping_cart =

shopping_goods = [["book",20],["cup",30],["pen",38],["laptop",4000],["phone",3800],

["watch",2800]]

salary = int(input("please input your salary:"))

while true:

for i in shopping_goods:

print(shopping_goods.index(i),",",i[0],":",i[1])

print("please select your goods")

command = input("please input the command:")

for i in shopping_goods:

if command.isdigit():

if int(command) == shopping_goods.index(i):

print("*******************")

if salary-i[1] >=0:

salary = salary - i[1]

print("your balance is :",salary)

else:

print("your balacne is low please choose another good")

if command == "exit":

print("your shopping cart is :",shopping_cart)

print("your balance is :",salary)

break

python 利用列表練習購物系統

輸入工資 salary int input 請輸入你的工資 列印商品列表 goods iphone 100 iphone2 200 iphone3 300 判斷是否可以購買,可以加入購物車,不可以提示 num 1 for temp in goods print num,temp 0 temp 1 n...

巢狀列表實現購物車專案

巢狀列表實現購物車專案 author administrator date 2019 10 25 購物車程式please input your salary 5880 購物清單如下 購物清單 1.iphone11 5800 2.mac book 9000 3.coffee 38 4.python b...

python列表的使用 Python 列表常見用法

列表索引 mlist 1,2,3,3,e 4,5 a a abc 5,6,7 print mlist print mlist 3 7 print mlist 3 1 print len mlist mlist 2 hi,python print mlist print mlist 3 6 print...