Python購物車的實現課程

2022-05-31 22:45:14 字數 3061 閱讀 8607

需求:

1.使用者輸入工資收入

2.列印商品列表

3.使用者選擇商品,不斷的加入購物車

4.檢測使用者餘額,直接捐款,不足提示餘額不足

5.允許主動退出,退出時,列印已購商品列表

重點方法:

列印列表下標的方法:

a=['alex','sys','root','admin']

>>> for index,i in enumerate(a):

...     print(index,i)

0 alex

1 sys

2 root

3 admin

1

#!/usr/bin/env python32#

-*-conding:utf-8-**3#

__author__:'liudong'

4 salary=input("

input your salary:")

5ifsalary.isdigit():

6 salary=int(salary)

7else

:8 exit("

invalid data type...")

9 welcom_msg='

welcome to shopping mall

'.center(50,'-'

)10print

(welcom_msg)

11 exit_flag=false

12 product_list =[

13 ('

iphone

',5000),

14 ('

mac air

',8000),

15 ('

mac pro

',9000),

16 ('

xiaomi

',20),

17 ('

coffe

',30),

18 ('

bike

',800),

19 ('

cloth

',200)20]

21 shop_car =

22while

notexit_flag:23#

for product_item in product_list:24#

p_name,p_price = product_item #可選的寫法

25print("

products list

".center(50,'-'

))26

#for p_name,p_price in product_list:27#

print(p_name,p_price) #由於此方法後面列印下標時,會變成2個無組,所以用下面的方法列印

28for item in

enumerate(product_list):

29 index=item[0]

30 p_name=item[1][0]

31 p_price=item[1][1]

32print(index,'.'

,p_name,p_price)

33 user_choice = input('

[q=quit,c=check]what do you want to buy?:')

34if user_choice.isdigit(): #

肯定是選商品

35 user_choice =int(user_choice)

36if user_choice

37 p_item =product_list[user_choice]

38if p_item[1] <= salary: #

買的起放入購物車

40 salary -= p_item[1] #

扣錢41

print('

added [%s] into your shop car,your current balance is \033[31;1m[%s]\033[0m

' %42

(p_item,salary)) #字型加顏色

43else:44

print('

your balance is [%s],cannot afford this product..

' %salary)

45else:46

if user_choice == 'q'

or user_choice == '

quit':

47print('

purchased products as blew:

'.center(40,'*'

))48

for item in

shop_car:

49print

(item)

50print('

end'.center(40,'*'

))51

print('

your balance is [%s]

' %salary)

52print('

bye.')

53 exit_flag =true

54elif user_choice == 'c'

or user_choice == '

check':

55print('

purchased products as blew:

'.center(40, '*'

))56

for item in

shop_car:

57print

(item)

58print('

your balance is \033[41;1m[%s]\033[0m

' % salary) #

背景加顏色

view code

程式設計的思維還要加強。。。。。

Python購物車的實現課程

需求 1.使用者輸入工資收入 2.列印商品列表 3.使用者選擇商品,不斷的加入購物車 4.檢測使用者餘額,直接捐款,不足提示餘額不足 5.允許主動退出,退出時,列印已購商品列表 重點方法 列印列表下標的方法 a alex sys root admin for index,i in enumerate...

Python實現購物車

encoding utf 8 定義列表商品 在這裡插入 片 product list iphone 100 mac 120 watch 218 bike 155 nike 299 adidas 266 shopping list 建立乙個空列表 salary input 使用者輸入工資 if sal...

python實現簡單購物車

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