Python購物車小程式

2021-09-11 19:17:33 字數 1503 閱讀 7578

問題描述:

商店有一系列商品,給出購買序列,然後輸入你的金額,然後選擇要購買的商品序號或是退出,如果持續購買餘額不足則提醒使用者餘額不足,然後如果使用者退出則列印使用者購買的商品列表,並列印使用者餘額。

# 購物車程式

product_list = [

('mac', 9000),

('kindle', 8000),

('tesla', 900000),

('python_book', 105),

('bike', 2000),

]shopping_car = # 購物車

money = input('please input your money') # 你的金錢

if money.isdigit():

money = int(money)

# for i in product_list:#方法一:

# print(product_list.index(i)+1,':',i)

## for i in enumerate(product_list,1): #方法二: enumerate()帶索引的列印 下標從1開始,不加1,預設從0開始。

# print(i)

while true:

for i, v in enumerate(product_list, 1): # 方法三: 方法二 列印的是乙個個元組,我們用兩個變數i,v接收元組

print(i, v)

choice = input('please input the num[exit:q]')

if choice.isdigit():

choice = int(choice)

if (choice >= 1) and (choice <= len(product_list)): # 求列表的長度

p_item = product_list[choice - 1]

p_price = float(p_item[1])

if p_price < money:

money -= p_price

else:

print('your money is not enough %s' % money)

print(p_item, p_price)

else:

print('編碼不存在!')

elif choice == 'q':

print('您購買的商品如下:')

for i, v in enumerate(shopping_car, 1): # 方法三: 方法二列印的是乙個個元組,我們用兩個變數i,v接收元組

print(i, v)

print('您還剩餘%s元' % money)

break

else:

print('input invalid')

else:

print('your input is not reasonable')

要點:

購物車小程式

coding utf 8 shp iphone 5000 offee 35 shoes 800 pric list e int input 請輸入賬戶金額 while true for i,p in enumerate shp print i,p 0 p 1 enther input 請輸入要購買的...

12 小程式 購物車

資料庫連表查詢json server 連線資料中的表 localhost 3000 carts?expand good 當前購物車商品的詳情資訊 命名要有統一性 商品資訊資料名 goods 購物車資料id goodid 購物車介面 localhost 3000 carts?expand goodsh...

小程式購物車實現

class cartmain for wx key key wx for index index class cart box data index 核取方塊 class icon if type success color rgb 51,138,238 size 20 bindtap select...