python3 練習題 購物車

2022-09-06 03:18:08 字數 1393 閱讀 7973

購物車程式

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

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

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

.使用者可一直購買商品,也可隨時退出,退出時列印已購的商和餘額

# 商品列表

product_list = [

('iphone8', 6888),

('macpro', 14800),

('小公尺6', 2499),

('bike', 800),

('coffee', 31),

('nike shoes', 799),

]# 購物車

shopping_list =

salary = input("輸入您的薪水:")

if salary.isdigit(): # 檢測字串是否只由數字組成

salary = int(salary)

run_flag = true

while run_flag:

print(str.center('商品列表', 30, '-'))

for k, v in enumerate(product_list):

print('%s. %s %s' % (k, v[0], v[1]))

choice = input("請輸入想買商品的編號,退出請輸入q:")

if choice.isdigit():

choice = int(choice)

print(choice)

if choice >= 0 and choice < len(product_list):

item_price = product_list[choice]

if item_price[1] <= salary: #買得起

salary -= item_price[1]

print("%s新增到購物車,您當前餘額是:%d" % (item_price, salary))

else:

print('您當前餘額不足')

else:

print('商品不存在')

elif choice == 'q' or choice == 'q':

if len(shopping_list) > 0:

print(str.center('您已購買以下商品', 30, '-'))

for k, v in enumerate(shopping_list):

print('%s. %s %s' % (k, v[0], v[1]))

print("您當前餘額:%d" % salary)

run_flag = false

python 3 6練習題 仿購物車

opop iphone 9800 bike 800 mac pro 12000 定義商品列表 pyhon book 120 telas 429800 memory 7000 hard 4000 upan 90 shopping list salary input 請輸入你的工資 輸入工資 if sa...

Python3 簡單購物車

實現了從products.csv中讀取商品資訊,購買後存入shopping list.csv中。第一次購買輸入salary,以後每次購買從shopping list.csv中獲取餘額。coding utf 8 author sxq import csv 讀產品 with open products....

乙個簡單購物車的練習題

author hemq product list 蘋果手機 5800 蘋果電腦 9800 自行車 8000 手錶 10600 咖啡 31 python書籍 120 shopping list salary input 請輸入您的工資 if salary.isdigit salary int sala...