Python3 簡單購物車

2021-08-22 11:36:51 字數 3571 閱讀 9016

實現了從products.csv中讀取商品資訊,購買後存入shopping_list.csv中。

第一次購買輸入salary,以後每次購買從shopping_list.csv中獲取餘額。

# -*- coding: utf-8 -*-

# @author: sxq

import csv

# 讀產品

with open("products.csv", "r", encoding = "utf-8") as f:

reader = csv.reader(f)

product_list = [row for row in reader]

# 讀賬戶餘額,檔案最後一行

with open("shopping_list.csv", "r", encoding = "utf-8") as csvfile:

mlines = csvfile.readlines()

if not mlines:

print('your current balance is none')

salary = input('input your salary:')

if salary.isdigit():

salary = int(salary)

else:

salary = mlines[-1].split(',')

salary = int(salary[1])

shopping_list = # 購物車

print('your current balance: '.format(salary))

while salary > 0:

for index, item in enumerate(product_list):

print(index, item)

user_choice = input('選擇要買的商品》:')

if user_choice.isdigit(): # 輸入商品編號是數字

user_choice = int(user_choice)

if user_choice < len(product_list) and user_choice >= 0:# 輸入的數字在商品編碼內

p_item = product_list[user_choice]

if int(p_item[1]) <= salary: # 買得起

salary -= int(p_item[1])

print('added %s into shopping cart, your current balance is \033[31;1m%s\033[0m' % (p_item, salary))

else: # 買不起

print('\033[41;1m你的餘額只剩[%s]了!\033[0m' % salary)

else: # 輸入的數字不在商品編碼內

print('product code [%s] is not exist!' % user_choice)

elif user_choice == 'q': # 輸入退出

print('-----------------shopping list------------------')

for p in shopping_list:

print(p)

print('your current balance:', salary)

with open("shopping_list.csv", "a", encoding="utf-8" ,newline = '') as f:

csv_writer = csv.writer(f)

csv_writer.writerow(['products', 'price'])

csv_writer.writerows(shopping_list)

csv_writer.writerow(['current balance',salary])

exit()

else: # 輸入無效字元

print('invalid option')

第一次購買:

your current balance is none

input your salary:10000

your current balance: 10000

0 ['iphonex', '8800']

1 ['mac pro', '9800']

2 ['bick', '800']

3 ['watch', '10600']

4 ['kfc', '31']

5 ['book', '20']

選擇要買的商品》:0

added ['iphonex', '8800'] into shopping cart, your current balance is 1200

0 ['iphonex', '8800']

1 ['mac pro', '9800']

2 ['bick', '800']

3 ['watch', '10600']

4 ['kfc', '31']

5 ['book', '20']

選擇要買的商品》:2

added ['bick', '800'] into shopping cart, your current balance is 400

0 ['iphonex', '8800']

1 ['mac pro', '9800']

2 ['bick', '800']

3 ['watch', '10600']

4 ['kfc', '31']

5 ['book', '20']

選擇要買的商品》:q

-----------------shopping list------------------

['iphonex', '8800']

['bick', '800']

your current balance: 400

繼續購買:

your current balance: 400

0 ['iphonex', '8800']

1 ['mac pro', '9800']

2 ['bick', '800']

3 ['watch', '10600']

4 ['kfc', '31']

5 ['book', '20']

選擇要買的商品》:4

added ['kfc', '31'] into shopping cart, your current balance is 369

0 ['iphonex', '8800']

1 ['mac pro', '9800']

2 ['bick', '800']

3 ['watch', '10600']

4 ['kfc', '31']

5 ['book', '20']

選擇要買的商品》:q

-----------------shopping list------------------

['kfc', '31']

your current balance: 369

python3 練習題 購物車

購物車程式 需求 啟動程式後,讓使用者輸入工資,然後列印商品列表 允許使用者根據商品編號購買商品 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 使用者可一直購買商品,也可隨時退出,退出時列印已購的商和餘額 商品列表 product list iphone8 6888 macpro 14...

簡單購物車

簡單購物車,要求如下 實現列印商品詳細資訊,使用者輸入商品名和購買個數,則將商品名,購買個數加入購物列表,如果輸入為空或其他非法輸入則要求使用者重新輸入msg dic good l while true for k in msg dic print k,msg dic k name input 商品...

python實現簡單購物車

encoding utf 8 author xianyt vertion python3 date 20180723 21 模擬實現選購商品 1 列出所有商品的編號 名稱和 2 選擇多個商品 3 檢視已經選擇的商品 單價 小計 和 總價 4 支付 輸入實付金額 折扣,輸出購物清單 總計 實付 找零 ...