需求:

  用户入口:

  1、商品信息放在文件中,从文件中读取

  2、已购商品,余额记录,第一要输入起始金额,以后不需要二次输入

  商家入口:

  2、可以添加商品,修改商品价格

 # Author:Lee Sirimport sys

 def goods_info():
goods_info = 'goods_info.txt'
fd = open(goods_info)
return fd def goods_list(fd):
print('商品列表'.center(30,'-'))
for items in fd.readlines():
index,goods_name,goods_price = items.split()
print(index,goods_name,goods_price) def check_cart(buylist):
print('您的购物车如下:')
for name,price in buylist:
print('名称:',name,'价格:',price)
check_choice = input('是否结账?:[Y/N]')
if check_choice == 'y' or check_choice == 'Y':
return True
else:
return False def check_money(money,buylist):
money = int(money)
chech_result = 0
for name,price in buylist:
chech_result += int(price)
if chech_result <= money:
return True,money - chech_result
else:
return False,money def buyer():
buylist = []
while True:
money = input("请输入的金钱:")
if money.isdigit():
while True:
money = int(money)
fd = goods_info()
goods_list(fd)
choice = input('请输出你要购买的商品序号:')
if choice.isdigit():
choice = int(choice)
# print(type(choice))
fd.seek(0)
if choice > 0 and choice <= len(fd.readlines()):
fd.seek(0)
#print('1')
for items in fd.readlines():
index, goods_name, goods_price = items.split()
if int(index) == choice:
buylist.append((goods_name,goods_price))
print('添加 %s 至购物车。' % (goods_name))
buy_choice = input('是否继续购买? [Y/N]')
if buy_choice == 'y' or buy_choice == 'Y':
continue
else:
if len(buylist) > 0:
if check_cart(buylist):
check_result, balance = check_money(money,buylist)
if check_result:
exit('结算成功,您的余额还有 %s' % balance )
else:
continue_buy = input('您的余额不足,是否修改购物车?: [Y/N]')
if continue_buy == 'Y' or continue_buy == 'y':
print('您的购物车如下:')
for index,buy_name in enumerate(buylist):
name,price = buy_name
print('序号:',index,'名称:', name, '价格:', price)
else:
while True:
dele_cart = input('请输入你要删除的产品序号:')
if dele_cart.isdigit():
dele_cart = int(dele_cart)
if dele_cart >= 0 and dele_cart <= len(buylist):
buylist.remove(dele_cart)
break
else:
print('输入错误请重新输入')
else:
print('输入错误,请重新输入') else:
continue_check = input('您的购物车为空,是否继续购买? [Y/N]')
if continue_check == 'Y' or continue_check == 'y':
continue
else:
exit('See you tomorrow!')
else:
print('输入有误,请重新输入:') else:
exit('您没有购买任何产品,再见!')
else:
print('输入有误,请重新输入') buyer()

Python小程序之购物车的更多相关文章

  1. 怎么样通过编写Python小程序来统计测试脚本的关键字

    怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...

  2. 微信小程序 - 实现购物车结算

    示例源码下载:小程序-实现购物车结算

  3. Python 小程序,对文件操作及其它

    以下是自己写的几个对文件操作的小程序,里面涉及到文件操作,列表(集合,字典)的运用等.比方说,从文件里读取一行数据.分别存放于列表中,再对列表进行操作.如去掉里面的反复项.排序等操作. 常见对文件里行 ...

  4. 微信小程序实战 购物车功能

    代码地址如下:http://www.demodashi.com/demo/12400.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/ ...

  5. Day1:第一个python小程序

    Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...

  6. 微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...

  7. 一个有意思的Python小程序(全国省会名称随机出题)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近比较迷Python,仿照<Python编程快速上手>8.5写了一个随机出卷的小 ...

  8. 微信小程序——加入购物车弹层

    对于网上商城,加入购物车是一个必备功能了.俺今天就来说下在微信小程序里如何造一个购物车弹层. 先上图: 主要用到的微信API:wx.createAnimation(OBJECT) 说下思路: 1.wx ...

  9. [转]微信小程序之购物车 —— 微信小程序实战商城系列(5)

    本文转自:http://blog.csdn.net/michael_ouyang/article/details/70755892 续上一篇的文章:微信小程序之商品属性分类  —— 微信小程序实战商城 ...

随机推荐

  1. Autofac小例子

    AutoFac是.net平台下的IOC容器产品.今天学习一下它的使用方法. 1.最简单的使用. public interface ITestDao { string SayHello(); } pub ...

  2. [P2387魔法森林

    题面 题意: 给出一个图,边权有两维,a与b. 求1到n的一条路径使得路径经过的边的最大的a与b的和最小,输出最小之和. \(Solution:\) 如果做过这题,那么就显得很简单了很好想了. 又是想 ...

  3. python基础训练营06

    任务六 时长: 啥是佩奇代码复现 参考链接:https://mp.weixin.qq.com/s/whtJOrlegpWzgisYJabxOg 画一只佩奇: 代码: from turtle impor ...

  4. TensorFlow 同时调用多个预训练好的模型

    在某些任务中,我们需要针对不同的情况训练多个不同的神经网络模型,这时候,在测试阶段,我们就需要调用多个预训练好的模型分别来进行预测. 调用单个预训练好的模型请点击此处 弄明白了如何调用单个模型,其实调 ...

  5. 条件随机场CRF

    条件随机场(CRF)是给定一组输入随机变量X的条件下另一组输出随机变量Y的条件概率分布模型,其特点是假设输出随机变量构成马尔科夫随机场.实际上是定义在时序数据上的对数线性模型.条件随机场属于判别模型. ...

  6. POJ 3565 Ants(最佳完美匹配)

    Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on ...

  7. 山科 STUST OJ Problem B: 编写函数:String to Double (II) (Append Code)

    这道题没啥别的毛病,我的错误在于看不懂题. 另外还有一点是注意浮点数存在-0 #include <stdio.h> #include <ctype.h> #include &l ...

  8. CSS position属性---absolute与relative

    详情请点击此链接 http://www.cnblogs.com/polk6/archive/2013/07/26/3214847.html

  9. [剑指Offer] 35.数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...

  10. [剑指Offer] 12.数值的整数次方

    [思路1]递归 class Solution { public: double Power(double base, int exponent) { ){ /base; exponent = -exp ...