python(3) 之简单购物车购物 (练习基础语法)

#购物小程序

用户启动时先输入工资

用户启动程序后打印商品列表

允许用户选择购买商品

允许用户不断的购买各种商品

购买时检测 余额是否足够,如果足够,直接扣款,否则打印余额不足

允许用户主动退出 程序,退出时,打印已购买商品列表

代码:

!/usr/bin/env python
# _*_ coding:utf-8 _*_

# 输入工资
salary = input("Input your salary:")
if salary.isdigit():      #Python isdigit() 方法检测字符串是否只由数字组成,不考虑是小数的情况。
    salary = int(salary)
else:
    exit("Inbaild data type...")

welcome_msg = "Welcome to Au Shopping"
print(welcome_msg)

exit_flag = False       #循环判断条件
product_list = [         #商品列表
    ("iphone",5888),
    ("Lenovo",6666),
    ("Mi 6 ",2999),
    ("Bike",688),
    ("Coffee",40),
    ("Car",999999),
    ("cloths",200),
]

shop_car = []    #购物车

#   当输入q或者quit时while循环结束 退出程序
while not exit_flag:       #   while exit_flag is not True:   
    #for product_item in product_list:
    #   p_name,p_price = product_item       
    print("product list".center(50,"-")) 
    #for p_name,p_price in product_list:
    #    print(p_name,p_price)               没有排序
    for item in enumerate(product_list):  #通过枚举函数打印输出排序后商品列表
        index = item[0]
        p_name = item[1][0]
        p_price = item[1][1]
        print(index,".",p_name,p_price)

    user_chioce = input("[q=quit,c=check]What do you want to buy : ")
    if user_chioce.isdigit():          #判断是选商品(输入的是数字)
        user_chioce = int(user_chioce)       
        if user_chioce < len(product_list ):   # 输入数字正确
            p_item = product_list[user_chioce]
            if p_item[1] <= salary:            #买得起
                shop_car.append(p_item)    #放入购物车
                salary -= p_item[1]       #扣钱
                #加颜色 033[32;1m[%s]\033[0m
                print("Added \033[32;1m[%s]\033[0m into shop car,you current balance is \033[32;1m[%s]\033[0m"%(p_item,salary))
            else:              #买不起
                print("Your balance is \033[31;1m[%s]\033[0m,cannot afford this..."%salary)
        else:       #输入数字不正确
            print("\033[31;1m check you input!\033[0m")

    else:     #不是输入数字
        if user_chioce == "q" or user_chioce =="quit":   #输入q或者quit 退出
            print("purchased products as below".center(40,"*"))
            for item in shop_car:        #打印购物车中商品列表
                print(item)
            print("END".center(40,"*"))
            print("Your balance was \033[32;1m[%s]\033[0m"%salary)
            print("Welcome to you next time~~~")
            exit_flag = True           # while循环条件改变
            
        # 输入c或者check 查看购物车商品列表
        elif user_chioce == "c" or user_chioce =="check":   
            print("purchased products as below".center(40, "*"))
            for item in shop_car:
                print(item)
            print("END".center(40, "*"))
            print("Your balance was \033[32;1m[%s]\033[0m" % salary)
        else:   #输入无效字符
            print("\033[31;1mcheck you input!\033[0m")

运行结果:

Input your salary:10000
Welcome to Au Shopping
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : a
check you input!
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 7
 check you input!
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 0
Added [('iphone', 5888)] into shop car,you current balance is [4112]
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 1
Your balance is [4112],cannot afford this...
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 3
Added [('Bike', 688)] into shop car,you current balance is [3424]
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 4
Added [('Coffee', 40)] into shop car,you current balance is [3384]
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 4
Added [('Coffee', 40)] into shop car,you current balance is [3344]
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : c
******purchased products as below*******
('iphone', 5888)
('Bike', 688)
('Coffee', 40)
('Coffee', 40)
******************END*******************
Your balance was [3344]
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : 6
Added [('cloths', 200)] into shop car,you current balance is [3144]
-------------------product list-------------------
0 . iphone 5888
1 . Lenovo 6666
2 . Mi 6  2999
3 . Bike 688
4 . Coffee 40
5 . Car 999999
6 . cloths 200
[q=quit,c=check]What do you want to buy : q
******purchased products as below*******
('iphone', 5888)
('Bike', 688)
('Coffee', 40)
('Coffee', 40)
('cloths', 200)
******************END*******************
Your balance was [3144]
Welcome to you next time~~~

未实现功能:

    购买时允许用户选择购买多少件

    允许多用户登陆,下一次登陆后,继续按上次的余额购买

    允许用户查看之前的购买记录

    显示已购买商品时,如果有重复的商品,不打印多行,显示购买数量

 

 

你可能感兴趣的:(编程,python)