2-15 购物车程序

1.需求

数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]

功能要求:
基础要求:

1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示


扩展需求:

1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

2、允许查询之前的消费记录

 

2.普通流程图

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

product = [['Iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]]
shopping_cart = []

flag = False  # 标志位
while not flag:
    print("----------商品列表 --------")
    for index, item in enumerate(product):
        msg = "%s. %s   %s" % (index, item[0], item[-1])
        print(msg)
    choice = input("输入你要买的商品编号|退出q :")
    if choice.isdigit():
        choice = int(choice)
        if choice < len(product):
            shopping_cart.append(product[choice])
            print('-----你购买了',product[choice])
        else:
            print('你输入的商品不存在')
    elif choice == 'q':
        if len(shopping_cart) > 0:
            print("------你的购物车---------")
            for index, item in enumerate(shopping_cart):
                msg = "%s. %s   %s" % (index, item[0], item[-1])
                print(msg)
        flag = True
        # break
    else:
        print('你输入的有误,请重新输入')

 

 

3.流程图

     

 

4.基本需求版本

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

goods = [
         {"name": "电脑", "price": 1999},
         {"name": "鼠标", "price": 10},
         {"name": "游艇", "price": 20},
         {"name": "美女", "price": 998}
         ]

shopping_cart = []
_username = 'alex'
_password = '123'

while True:     # 用户名密码循环1
    username = input("请您输入用户名:").strip()
    password = input("请您输入密码:").strip()
    if username == _username and password == _password:
        print("\033[1;34m-----登录成功,欢迎%s\033[0m"%username)
        while True:     # 工资循环2
            salary = input("请输入你的工资:").strip()
            if not salary:
                continue
            if salary.isdigit():
                salary = int(salary)
                while True:     # 商品列表循环3
                    print("----------商品列表 --------")
                    for index, item in enumerate(goods):
                        print("%s   %s  %s" % (index, item['name'], item['price']))
                    choice = input("输入你要买的商品编号|退出q :").strip()
                    if not choice:
                        continue
                    if choice.isdigit():
                        choice = int(choice)
                        if choice < len(goods):
                            if salary >= goods[choice]['price']:
                                shopping_cart.append([goods[choice]['name'], goods[choice]['price']])  # 加入商品名,price
                                print('\033[1;32m>你购买了%s\033[0m'%goods[choice]['name'])
                                salary -= goods[choice]['price']
                                print('\033[1;31m>余额剩余%s\033[0m'%salary)
                            else:
                                print("\033[1;31m余额不足,请重新选择\033[0m")
                        else:
                            print('\033[1;31;47m你输入的商品不存在\033[0m')
                    elif choice == 'q':
                        if len(shopping_cart) > 0:
                            print("\033[1;34m------你的购物车---------")
                            for index, item in enumerate(shopping_cart):
                                print(index, item[0], item[-1])
                            print("------------------------")
                            print("你的余额:%s\033[0m"%salary)
                            exit()
                        else:
                            print("\033[1;34;47m你的购物车为空,你的余额:%s\033[0m"%salary)
                            exit()
                    else:
                        print('\033[1;31;47m你输入的有误,请重新输入\033[0m')
            else:
                print('\033[1;31m你输入的有误,请重新输入\033[0m')
    else:
        print("\033[1;31;47m用户名或密码错误\033[0m")

 

 

5.高亮显示 

我们可以通过对有用的信息设置不同颜色来达到醒目的效果,因为我平时都是在linux下开发,
而linux终端中的颜色是用转义序列控制的,转义序列是以ESC开头,可以用\033完成相同的工作(ESC的ASCII码用十进制表示就是27,等于用八进制表示的33)。

 

显示颜色格式:\033[显示方式;字体色;背景色m......[\033[0m

print('This is a \033[1;35m test \033[0m!')
print('This is a \033[1;32;43m test \033[0m!')
print('\033[1;33;44mThis is a test !\033[0m')

  

 

-------------------------------
显示方式     |      效果
-------------------------------
0           |     终端默认设置
1           |     高亮显示
4           |     使用下划线
5           |     闪烁
7           |     反白显示
8           |     不可见
-------------------------------

 

-------------------------------------------
-------------------------------------------               
字体色     |       背景色     |      颜色描述
-------------------------------------------
30        |        40       |       黑色
31        |        41       |       红色
32        |        42       |       绿色
33        |        43       |       黃色
34        |        44       |       蓝色
35        |        45       |       紫红色
36        |        46       |       青蓝色
37        |        47       |       白色
-------------------------------------------

 

 

6.扩展需求

# -*- coding:utf-8 -*-
import time

goods = [
         {"name": "电脑", "price": 1999},
         {"name": "鼠标", "price": 10},
         {"name": "游艇", "price": 20},
         {"name": "美女", "price": 998}
         ]
shopping_cart = []
_username = 'alex'
_password = '123'

while True:     # 用户名密码循环1
    username = input("\033[1;32m请您输入用户名:\033[0m").strip()
    password = input("\033[1;32m请您输入密码:\033[0m").strip()
    if username == _username and password == _password:
        print("\033[1;34m-----登录成功,欢迎%s\033[0m"%username)

        while True:     # 工资循环2
            with open('salary', 'r') as f1:
                salary = f1.read()
            if salary:
                print('\033[1;31m你的余额还有:%s\033[0m' % salary)
            else:
                salary = input("\033[1;32m请输入你的工资:\033[0m").strip()
                if not salary:
                    continue
            if salary.isdigit():
                salary = int(salary)
                with open('salary', 'w') as f2:
                    f2.write(str(salary))

                while True:     # 商品列表循环3
                    print("----------商品列表 --------")
                    for index, item in enumerate(goods):
                        print("%s   %s  %s" % (index, item['name'], item['price']))
                    choice = input("\033[1;34m输入你要买的商品编号|查看消费记录b|退出q:\033[0m").strip()
                    if not choice:
                        continue
                    if choice.isdigit():
                        choice = int(choice)
                        if choice < len(goods):
                            if salary >= goods[choice]['price']:
                                shopping_cart.append([goods[choice]['name'], goods[choice]['price']])
                                # 消费记录加入文件
                                with open('shopping_records', 'a') as f:
                                    now_time = time.ctime()
                                    goods_choice = [goods[choice]['name'], goods[choice]['price']]
                                    record = str(now_time) + '\t' + str(goods_choice) + '\n'
                                    f.write(record)

                                print('\033[1;32m>你购买了%s\033[0m'%goods[choice]['name'])
                                salary -= goods[choice]['price']
                                print('\033[1;31m>余额剩余%s\033[0m'%salary)
                            else:
                                print("\033[1;31m余额不足,请重新选择\033[0m")
                        else:
                            print('\033[1;31m你输入的商品不存在\033[0m')

                    elif choice == 'b':
                        with open('shopping_records', 'r') as f:
                            records = f.read()
                            if len(records):
                                print('-------消费记录------')
                                print(records)
                            else:
                                print('\033[1;31m>>你还没有买过东西\033[0m')

                    elif choice == 'q':
                        if len(shopping_cart) > 0:
                            print("\033[1;32m------你的购物车---------")
                            for index, item in enumerate(shopping_cart):
                                print(index, item[0], item[-1])
                            print("------------------------")
                            print("你的余额:%s\033[0m"%salary)

                            with open('salary', 'w') as f2:
                                f2.write(str(salary))
                            exit()
                        else:
                            print("\033[1;31m你的购物车为空,你的余额:%s\033[0m"%salary)
                            exit()

                    else:
                        print('\033[1;31;47m你输入的有误,请重新输入\033[0m')
            else:
                print('\033[1;31m你输入的有误,请重新输入\033[0m')
    else:
        print("\033[1;31;47m用户名或密码错误\033[0m")

 

 

8.修改后 

 

# -*- coding:utf-8 -*-
import time

goods = [
         {"name": "电脑", "price": 1999},
         {"name": "鼠标", "price": 10},
         {"name": "游艇", "price": 20},
         {"name": "美女", "price": 998}
         ]
shopping_cart = []
_username = 'alex'
_password = '123'
count = 0

while count<3 :     # 用户名密码循环1
    username = input("\033[1;32m请您输入用户名:\033[0m").strip()
    password = input("\033[1;32m请您输入密码:\033[0m").strip()
    if username == _username and password == _password:
        print("\033[1;34m-----登录成功,欢迎%s\033[0m"%username)

        # 得到工资
        with open('salary', 'r') as f1:
            data = f1.read()
        if data:
            salary = float(data)
            print('\033[1;31m你的余额还有:%s\033[0m' % salary)
        else:
            while True:     # 工资循环2
                salary = input("\033[1;32m请输入你的工资:\033[0m").strip()
                if salary.isdigit():   # 只能够把 3456转换,不能转换 3456.444,
                    salary = float(salary)
                    break
                else:
                    print('你输入的有误,请重新输入')

        while True:     # 商品列表循环3
            print("----------商品列表 --------")
            for index, item in enumerate(goods):
                print("%s   %s  %s" % (index, item['name'], item['price']))
            choice = input("\033[1;34m输入你要买的商品编号|查看消费记录b|退出q:\033[0m").strip()
            if choice.isdigit():
                choice = int(choice)
                if choice < len(goods):
                    if salary >= float(goods[choice]['price']):
                        shopping_cart.append([goods[choice]['name'], goods[choice]['price']])
                        # 消费记录加入文件
                        with open('shopping_records', 'a') as f:
                            now_time = time.ctime()
                            goods_choice = [goods[choice]['name'], goods[choice]['price']]
                            record = str(now_time) + '\t' + str(goods_choice) + '\n'
                            f.write(record)

                        print('\033[1;32m>你购买了%s\033[0m'%goods[choice]['name'])
                        salary -= float(goods[choice]['price'])
                        print('\033[1;31m>余额剩余%s\033[0m'%salary)
                    else:
                        print("\033[1;31m余额不足,请重新选择\033[0m")
                else:
                    print('\033[1;31m你输入的商品不存在\033[0m')

            elif choice == 'b':
                with open('shopping_records', 'r') as f:
                    records = f.read()
                if len(records):
                    print('-------消费记录------')
                    print(records)
                else:
                    print('\033[1;31m>>你还没有买过东西\033[0m')

            elif choice == 'q':
                if len(shopping_cart) > 0:
                    print("\033[1;32m------你的购物车---------")
                    for index, item in enumerate(shopping_cart):
                        print(index, item[0], item[-1])
                    print("------------------------")
                    print("你的余额:%s\033[0m"%salary)

                    with open('salary', 'w') as f2:
                        f2.write(str(salary))
                    exit()
                else:
                    print("\033[1;31m你的购物车为空,你的余额:%s\033[0m"%salary)
                    with open('salary', 'w') as f2:
                        f2.write(str(salary))
                    exit()

            else:
                print('\033[1;31;47m你输入的有误,请重新输入\033[0m')

    else:
        print("\033[1;31;47m用户名或密码错误\033[0m")

    count += 1

print("you have try more times")

 

转载于:https://www.cnblogs.com/venicid/p/8387711.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 微信小程序的checkbox-group可以用来实现购物车功能。购物车通常会展示用户已选择的商品,并且允许用户对选择的商品进行编辑、删除等操作。 首先,我们可以使用checkbox-group组件来展示用户可选的商品列表。每个商品可以用一个checkbox和相应的label展示。用户可以通过勾选checkbox来选择要购买的商品。 在选择完商品后,我们可以通过checkbox-group的bindchange事件来监听用户的选择变化。在该事件的处理函数中,我们可以获取到用户选择的商品的信息。可以将这些信息保存在一个数组中,用于在购物车中展示已选择的商品。 接下来,我们可以使用一个列表展示购物车中的商品。在列表中,我们可以展示商品的图片、名称、价格等信息。同时,我们还可以在每个商品后面添加一个删除按钮,供用户删除不再需要的商品。 当用户点击删除按钮时,我们可以根据商品的索引或唯一标识从购物车中移除该商品,并更新购物车的展示。 除了展示已选择的商品和删除商品,我们还可以在购物车中展示商品的数量,并提供加减按钮供用户调整商品的数量。用户可以点击加减按钮来增加或减少商品的数量。同时,我们还可以根据商品的数量和价格计算出该商品的小计,并在购物车中展示。 最后,我们可以在购物车底部添加一个结算按钮,供用户点击进入结算页面。用户可以在结算页面确认购买的商品和总金额,并填写收货地址等信息。 总之,通过微信小程序的checkbox-group组件,我们可以很方便地实现购物车功能,让用户可以方便地选择和管理购买的商品。 ### 回答2: 微信小程序的checkbox-group组件可以用于实现购物车的功能。首先,在页面的wxml文件中,可以使用checkbox-group组件来创建一个购物车列表,每个商品项都有一个对应的checkbox。 例如: ``` <checkbox-group bindchange="checkboxChange"> <block wx:for="{{cartList}}"> <checkbox value="{{item.id}}"></checkbox> <view>{{item.name}}</view> <view>{{item.price}}</view> </block> </checkbox-group> ``` 在js文件中,需要定义一个cartList数组来存储购物车中的商品信息,并添加相应的事件处理函数checkboxChange来实现选中和取消选中商品的操作。 例如: ``` Page({ data: { cartList: [ { id: 1, name: "商品1", price: 10 }, { id: 2, name: "商品2", price: 20 }, // 其他商品项 ] }, checkboxChange(e) { console.log('checkbox发生change事件,携带value值为:', e.detail.value) // 可以在这里根据checkbox的选中状态来更新购物车中商品的选中状态 } }) ``` 在checkboxChange函数中,可以获取到checkbox选中的商品的value值,可以通过遍历cartList数组来找到对应的商品项,并更新选中状态。 最后,可以根据需求在购物车中添加结算按钮,点击结算按钮可以获取到购物车中选中的商品的信息,进行下一步的操作。 ### 回答3: 微信小程序中的checkbox-group组件可以用于实现购物车的功能。购物车一般用于将用户选择的商品添加到购物车中,方便用户统一管理和结算。 首先,在小程序中创建一个页面,页面上包含一个checkbox-group组件和一个结算按钮。checkbox-group组件用于展示用户可以选择的商品列表,每个商品都有对应的checkbox。用户可以通过勾选checkbox的方式选择要购买的商品,勾选的商品会被添加到购物车中。 接下来,通过小程序框架提供的数据绑定机制,将商品的勾选状态与数据关联起来。可以使用一个数组来保存用户选择的商品信息,数组中的每个元素包含商品的相关信息和一个表示勾选状态的标志位。当用户勾选或取消勾选某个商品的时候,可以通过改变数组中相应元素的标志位来表示选中或未选中的状态。 在结算按钮的点击事件中,遍历数组,筛选出被勾选的商品,然后可以进行相关的结算操作,如计算总价、生成订单等。 在购物车页面中,可以展示被勾选的商品列表,以供用户查看和管理。用户可以通过点击checkbox来修改商品的勾选状态,然后可以进行批量删除、修改数量等操作。 最后,在小程序中加入其他必要的功能,如商品的增加、删除、数量修改等,以供用户更好地管理购物车。 总之,通过使用微信小程序中的checkbox-group组件,我们可以轻松实现购物车功能,方便用户管理和结算商品。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值