[ Python -1 ] 简易购物车程序
练习:
1. 要求用户输入总资产,例如:2000
2. 显示商品列表,让用户根据序号选择商品,加入购物车
3. 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [{'name':'电脑', 'price':1999},{'name':'鼠标', 'price':10},{'name':'游艇', 'price':20},{'name':'玩具', 'price':998},]
p1 = int(input('enter your money:'))
while True:
if not isinstance(p1, int):
raise TypeError('bad type.')
for i in goods:
print(goods.index(i), i)
p2 = int(input('enter your price:'))
if p2 == 0:
p1 = p1 - int(goods[p2]['price'])
if p1 < 0:
print('余额不足。')
break
else:
print('***********************')
print('当前余额:', p1)
print('***********************')
elif p2 == 1:
p1 = p1 - int(goods[p2]['price'])
# print(p3)
if p1 < 0:
print('余额不足。')
break
else:
print('***********************')
print('当前余额:', p1)
print('***********************')
elif p2 == 2:
p1 = p1 - int(goods[p2]['price'])
# print(p3)
if p1 < 0:
print('余额不足。')
break
else:
print('***********************')
print('当前余额:', p1)
print('***********************')
elif p2 == 3:
p1= p1 - int(goods[p2]['price'])
# print(p3)
if p1 < 0:
print('余额不足。')
break
else:
print('***********************')
print('当前余额:', p1)
print('***********************')
else:
print('***********************')
print('商品序号错误,请重新输入。')
print('***********************')
主要练习点:
1. 列表(list)、字典(dist) 类型灵活运用。
2. 条件判断while 、 if ... break
修改一版:
# -*- coding: utf-8 -*-
goods_list = [
['computer', 5000],
['apple', 500],
['pen', 50],
]
salary = float(input('enter your salary:'))
while True:
for index, item in enumerate(goods_list, 1):
print(index, item)
choice = input('enter your choice:')
if choice == 'q':
break
if choice.isdigit() == False:
print('\033[31;1m输入编号错误,请重新输入\033[1m')
elif int(choice) > len(goods_list) or int(choice) < 1:
print('\033[31;1m编号不在商品列表中\033[1m')
else:
choice_buy = int(choice) -1
if salary >= goods_list[choice_buy][1]:
print('\033[32;1m购买成功.\033[1m')
salary = salary - goods_list[choice_buy][1]
else:
print('\033[31;1m购买失败\033[1m')
break
[ Python -1 ] 简易购物车程序的更多相关文章
- Python编写简易木马程序(转载乌云)
Python编写简易木马程序 light · 2015/01/26 10:07 0x00 准备 文章内容仅供学习研究.切勿用于非法用途! 这次我们使用Python编写一个具有键盘记录.截屏以及通信功能 ...
- python学习:购物车程序
购物车程序 product_list = [ ('mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike', ...
- python元组与购物车程序
#Author:zww ''' 程序:购物车程序 需求: 1.启动程序后,让用户输入工资,然后打印呢商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否足够,够就直接扣款,不够 ...
- Python简易购物车程序
2019年5月24日,是我离开北京回学校的日子.我退了组的房子,辞了工作,带着对小秋香不舍回家,这匆匆一别,不知何时才是归期. 这几天都疏于学习,因为每一天我都会和她找点有意思的事情来做,如果这一别就 ...
- 【转载】Python编写简易木马程序
转载来自: http://drops.wooyun.org/papers/4751?utm_source=tuicool 使用Python编写一个具有键盘记录.截屏以及通信功能的简易木马. 首先准备好 ...
- python练习2 购物车程序
# -*- coding: utf-8 -*-# @Time : 2018/10/18 16:06# @Author : Two Brother# @EMAIL : yeluyide@163.com# ...
- ~~小练习:python的简易购物车~~
进击のpython 1,用户先给自己的账户充钱:比如先充3000元. 2,有如下的一个格式: goods = [{"name": "电脑", "pri ...
- Python初学时购物车程序练习实例
不多说了,直接上代码: #Author:Lancy Wu product_list=[ ('Iphone',5800), ('Mac Pro',9800), ('Bike', 800), ('Watc ...
- python复习购物车程序
个人学习总结: 无他,唯手熟尔!多敲多练才是王道 python 第三课 元组的灵活运用&字符串的诸多操作 Program01 '''时间 2018年2月12日12:15:28目的 购物车程序 ...
随机推荐
- Python 3 学习笔记之——数据类型
1. 数字 类型 int, float, bool, complex type() 查看变量类型 isinstance(a, int) 查看变量类型 运算符 % 取余 // 返回商的整数部分 ** 幂 ...
- VC中结构体的内存布局
看了 VC++中内存对齐 这篇文章,感觉说复杂了,根据我的总结,要算出结构体的内存大小和偏移量,只要清楚结构体各成员的内存布局就行了,下面介绍一下我总结的规则,有不对之处,欢迎回复. 1.实际PACK ...
- CentOS安装Harbor
CentOS版本:7.4 Harbor版本:1.5.0 Docker版本:1.12.6 Docker Compose版本:1.21.2 一.安装Harbor(http方式,80端口) 1.安装Dock ...
- mysql insert into select 语法
Insert into Table2(field1,field2,...) select value1,value2,... from Table1 这样就对了
- Python模块学习:logging 日志记录
原文出处: DarkBull 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...
- lintcode-57-三数之和
57-三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 注意事项 在三元组(a, b, c),要求a <= b <= ...
- javascript知识总结
javascript: 面对对象 函数创建方式: 1.工厂模式 function createPerson(name, age, job){ var o = new Object(); //创建工厂对 ...
- CSS clear both清除浮动总结
我们知道有时候使用了CSS float浮动,会产生CSS浮动,这个时候就需要清理浮动,我们就用clear样式属性即可实现. 接下来我们来认识与学习CSS clear知识与用法. clear清除浮动目录 ...
- Citrix Netscaler版本管理和选择
Citrix Netscaler版本管理和选择 来源 http://blog.51cto.com/caojin/1898164 随着Citrix Netscaler的快速发展,有很多人在维护设备时经常 ...
- [洛谷P2147][SDOI2008]洞穴勘测
题目大意:有$n$个洞穴,$m$条指令,指令有三种 $Connect\;u\;v$:在$u,v$之间连一条边 $Destroy\;u\;v$:切断$u,v$之间的边 $Query\;u\;v$:询问$ ...