Python3实现购物车功能
作者:爱跳舞的青年君 发布时间:2021-01-15 08:43:10
标签:python3,购物车
本文实例为大家分享了Python3实现购物车功能的具体代码,供大家参考,具体内容如下
购物车要求:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
逻辑图:
执行代码:
#!/usr/bin/env python3
# Author: Robert
# --*-- coding: utf-8 --*--
set = False #设置set 当输入为q就可以退出
file = open("购物车用户信息档案.txt","r+",encoding="utf-8") #读取购物车用户信息文件
f = str(file.read()) #将文件内容转化成字符串
for line in f:
file_str = str(f)
data = eval(file_str) #将字符串转换为字典data
name = input("输入姓名:")
password = input("输入密码:")
while True:
if name in data: #用户在档案中
if password in data[name]: #密码和用户名对应,校验正确,登录。
salary = float(data[name][password])
print('''\033[32;1m欢迎登录,当前余额为%s\033[0m'''%salary)
break
else: #否则密码错误,请重新输入
password = input("密码错误,请重新输入:")
continue
else: #否则判断为首次登录,将用户名,密码,工资存到用户信息文件中
password_salary = {}
salary_str = input("欢迎首次登录,请输入你的工资:")
salary = float(salary_str)
password_salary[password] = salary #工资对应到密码
data[name] = password_salary #将密码-工资对应到用户名
file.seek(0)
file.write(str(data))
file.tell()
break
list = [#购物清单
["iphone",5800],
["sifei",800],
["macbook",17500],
["book",75],
["apple",5]
]
file_list_r = open("历史购物信息.txt","r+",encoding="utf-8")
file_list_r = str(file_list_r.read())
shoppinglist_dict = eval(file_list_r)
if name not in shoppinglist_dict:
shoppinglist_dict[name] = []
shoppinglist = shoppinglist_dict[name]
shoppinglist_dict_now = []
choose = input("\n是否需要查询历史购物记录(y/n):")
if choose == 'y':
print("\n\n---------->历史购物记录<----------")
print(shoppinglist)
print("---------->结束<----------")
while not set: #购物车开始
print("---------->商品清单<----------")
for index,item in enumerate(list,1):
print(index,item)
print("---------->结束<----------")
number = input("请输入想购买商品编号:")
if number == "q":
set = True
data[name][password] = str(salary)
file.seek(0)
file.write(str(data))
file.tell()
print("---------->购物清单<----------")
print(shoppinglist)
print("您的余额:",salary)
print("---------->结束<----------")
shoppinglist.extend(shoppinglist)
shoppinglist_dict[name] = shoppinglist
elif number.isdigit() == False:
print("\033[31;1m输入不是编号内容,请重新输入\033[0m")
elif int(number)>int(len(list)) or int(number)<= 0: #输入值不在清单中,报错
print("\033[31;1m您所购买的商品不在清单中\033[0m")
else:
number_buy = int(number)-1
if list[number_buy][1]<(salary): #如果余额足够,提示购买成功并显示余额。
salary = salary - int(list[number_buy][1])
msg = '\033[32;1m您已经将%s加入购物车中,余额为%d\033[0m'%(list[number_buy][0],salary)
print(msg)
shoppinglist.append(list[number_buy]) #将本次购物信息加到购买记录中
else:
print("\033[31;1m余额不足,无法购买!\033[0m") #提示余额不足
购物车用户信息档案.txt
{'name': {'password': '10000'}, 'cx': {'123': '725.0'}, 'robert': {'qw': '440.0'}, 'cv1': {'1': 100.5}, 'ROBERT': {'QW': 1560.0}, 'qwe': {'qw': '1555.0'}}
历史购物信息.txt
{'name': [['iphone', 5800],['bike', 800]], 'cx':[['iphone', 5800],['apple', 5],['apple', 5], ['book', 75]]}
来源:https://blog.csdn.net/weixin_39099836/article/details/78389167
0
投稿
猜你喜欢
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 14 - Periodical and Intro
- 一、前言Python logging 模块定义了为应用程序和库实现灵活的事件日志记录的函数和类。程序开发过程中,很多程序都有记录日志的需求,
- 当我们采用s=[[0]*3]*2初始化一个数组,然后对s[0][0]进行赋值,改变的是第一列所有的值。因为用s = [[0]*3]*2 初始
- 建立资料表:Step1首先开启phpmyadmin,进入wordpress资料库中,并新增一个wp_gbook的资料表与栏位数目8。Step
- 2D坐标系1 修改全部坐标颜色import matplotlib.pyplot as pltimport numpy as np#显示静态图
- 1. 首先导入一些python画图的包,读取txt文件,假设我现在有两个模型训练结果的records.txt文件import numpy a
- 不知道用ASP写代码的朋友是不是和我有一样的感受,ASP中最头疼的就是调试程序的时候不方便,我想可能很多朋友都会用这样的方法&ldq
- 在python中,循环有一个语句:for语句。简单的for循环例子>>> hello = "world"
- 安装先下载源码,地址:ps://pypi.python.org/pypi/IPy/">https://pypi.python
- python调用文件时找不到相对路径current_path = os.path.dirname(__file__)if current_p
- Python生成指定数量的优惠码打开Python开发工具IDLE,新建‘codeGen.py'文件,并保存导入需要的包,这里需要ra
- 今天碰到这个极度郁闷的报错,搞了大半下午,才发现是ie的问题,忍不住大骂。例子是这样的:页面中有多处能出发菜单,并且菜单出现在触发点的旁边,
- 本文实例为大家分享了python实现自动抢火车票,供大家参考,具体内容如下splinter使用首先介绍一下splinter使用:plinte
- 单线程实现多个定时器NewTimer.py#!/usr/bin/env pythonfrom heapq import *from thre
- 如何导入数据数据可能有各种格式,虽然常见的是HDFS,但是因为在Python爬虫中数据库用的比较多的是MongoDB,所以这里会重点说说如何
- Python实现AES算法密码学课程老师留的作业,我觉得用python实现更简单,就用python写了一个加解密的程序。程序分成三个部分,一
- 对批量WAV音频进行等长分割对WAV格式的音频以相同长度进行分割。import osimport waveimport numpy as n
- 核心代码:#!/usr/bin/python# -*- coding: UTF-8 -*-import smtplibimport osim
- upyter Notebook已经逐渐取代IDE成为了多平台上写简单Python脚本或应用的几家选择。Jupyter Notebook可以通
- 什么是爬虫爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源