Python3 list与循环练习(购物车)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author;Tsukasa product_list = [
('Iphone',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Coffee',30),
('Tuskasa Python',20000),
] #创建一个商品列表
shopping_list = [] #创建一个购物车,空的list
salary = input('你的工资是多少:') #input让用户输入工资
if salary.isdigit():
salary = int(salary) #判断输入的是否数字,如果是的话改成int类型
while True: #进入循环
for index,list in enumerate(product_list): #enmuerate可以提取下标,提取product_list的下标,0.1.2.3....
print(index,list) #打印出商品列表的下标和list
user_choice = input('你要买什么商品:') #让用户输入购买商品的下标
if user_choice.isdigit():
user_choice = int(user_choice) #判断必须是数字,并int
if user_choice < len(product_list) and user_choice >=0:#设定输入范围,len()可以获取list的总数目,设定总数目为上线,并大于等于0
p_list = product_list[user_choice]#通过商品下标把商品取出来
if p_list[1] <= salary: #买得起、、,判断工资够
shopping_list.append(p_list)# 添加到shopping_list购物车
salary -= p_list[1]#扣工资
print('%s 已加入购物车,你现在还有%s元。'%(p_list,salary))
else:
print('没钱你买个J8啊')
exit()
else:
print('不要乱输入,请输入商品编号!') #没有商品下标
elif user_choice == "q":
print('-----shopping list-----')
for p in shopping_list:
print('你现在买了:',p)
print('你现在还剩%s元。' %(salary))
exit()
else:
print('不要乱输入,输入商品编号')
Python3 list与循环练习(购物车)的更多相关文章
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- Python3.5 Day2作业:购物车程序
需求: 1. 启动程序后,用户通过账号密码登录,然后打印商品列表. 2. 允许用户根据商品编号购买商品. 3. 用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒充值. 4. 可随时退出,退出 ...
- 1.3if判断语句+while和for循环语句+购物车作业
1.if 语句 if userame=_usename and password=_password: print("welcome user {name} login..."). ...
- Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)
一. 从第3层循环直接跳出所有循环 break_flag = False count = 0 while break_flag == False: print("-第一层") wh ...
- python练习-跳出多层循环和购物车
跳出多层循环:三层循环,最里层直接跳出3层 在Python中,函数运行到return这一句就会停止,因此可以利用这一特性,将功能写成函数,终止多重循环 def work(): for i in ran ...
- Python3基础 else 循环完整结束才执行
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- python3.4用循环往mysql5.7中写数据并输出
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...
- Python3 条件与循环
1.条件控制 下面是一个简单的条件控制语句 s=input('Please input a str: ') if s=='python': print('I love python!') elif s ...
- Python3 使用基本循环实现多级目录(思路)
一.多级目录设计: 1. 通过循环的方式显示菜单和进入菜单 2. 设置标志位以提供回退上一层菜单 2. 设置标志位以提供退出程序 二.注意要点: 1. 菜单样式,层次关系不要弄混乱 2. 当输入错误时 ...
随机推荐
- [Leetcode] first missing positve 缺失的第一个正数
Given an unsorted integer array, find the first missing positive integer. For example,Given[1,2,0]re ...
- BZOJ3648 寝室管理 【点分治 + 环套树】
3648: 寝室管理 Time Limit: 40 Sec Memory Limit: 512 MB Submit: 366 Solved: 152 [Submit][Status][Discus ...
- 【POJ 3169 Layout】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 12565Accepted: 6043 Description Like every ...
- BZOJ_day9
哇,一道巨大的水题害得我wa了无数次... 总结一下教训 大家一定记住(给我自己看的) 位运算 一定要加()!!! 重要的事情说三遍 位运算 一定要加()!!! 位运算 一定要加()!!! 位运算 ...
- [学习笔记]LCT进阶操作
LCT总结——应用篇(附题单)(LCT) 一般都是维护链的操作.split即可搞定. 进阶操作的话,处理好辅助树和原树的关系即可搞定. 其实,最大的区别就是,splay随便转,辅助树形态变了,但是原树 ...
- dns服务 很多问题,后续再研究
慕课网:http://www.imooc.com/video/5220 参考:http://jingyan.baidu.com/article/870c6fc32c028eb03fe4be30.htm ...
- P2765 魔术球问题(网络流24题)
题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...
- django自己搭建的博客
1.博客地址: http://jiangtao4.pythonanywhere.com/ 2.后台可以发布笔记,可以翻页,数据存在MySQL里面 3.GitHub地址: https://github. ...
- 着色方案(bzoj 1079)
Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...
- bzoj1861 书架 splay版
单点插入删除以及求前缀 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...