python基础:三层循环
三层循环基本演示:
break_flag = False #标记1
break_flag2 = False #标记2
break_flag3 = False #标记3
while not break_flag: #因为标记是False,所以是 not break_flag成立循环
print("the first layer is running...")
option=input(">>>[b:back, q:quit,c:continue]:").strip() #让买家选择,strip()去除输入时空格
if option == "b":
break_flag2 = True #第一层,没得退,所以break_flag2=true,不断打印'the first...'
elif option == "q":
break_flag = True #选择退出,把循环条件改为true,就能退出循环
else:
break_flag3,break_flag2 = False,False #既不是b,也不是q,则第二三层循环条件继续成立,往下走
while not (break_flag or break_flag2): #进入第二层,那么第一层循环条件必须是false,q时随时可以退出
print("in layer two...")
option=input(">>>[b:back, q:quit,c:continue]:").strip()
if option == "b":
break_flag2 = True #退到第一层,因为else:break_flag3,break_flag2 = False,False
elif option == "q":
break_flag = True #退出整个循环
else:
break_flag2,break_flag3 = False,False #这里可以实现第二层第三层的切换 while not(break_flag or break_flag2 or break_flag3): #与上面同理
print("in layer three...")
option=input(">>>[b:back, q:quit,c:continue]:").strip()
if option == "b":
break_flag3 = True
elif option == "q":
break_flag = True
文件内容查找和替换:
import os #os操作系统模块
f = open("test.txt","r") #旧文件读模式
f_new = open("text_new.txt","w") #新文件写模式
for line in f: #遍历旧文件
if line.startswith("alex"): #找到要替换的行
new_line = line.replace("alex","ALEX LI....") #替换好赋值给新行
f_new.write(new_line) #把新行写到新文件
else:
f_new.write(line) #如果找不到,则把原文写到新文件
f.close()
f_new.close()
os.rename("test.txt","test.txt.bak") #把旧文件改名为back up
os.rename("text_new.txt","test.txt") #新文件覆盖旧文件 '''
f = open("test.txt","r+")
for line in f:
if line.startswith("alex"):
new_line = line.replace("alex","ALEX LI")
print("current pos:",f.tell()) #显示指针的位置
f.seek(37) #把指针指到该位置
f.write(new_line)
break
f.close()
'''
购物车基本演示:
product_list = [
('Iphone6sPlus', 6888),
('MacBook', 11300),
('CookBook', 50),
('Coffee', 30),
('KindleFire', 1200),
('NB', 800),
]
user_asset = 10000 break_flag = False
shopping_cart = []
paid_list = []
while not break_flag:
for index,i in enumerate(product_list):
print(index,i[0],i[1])
user_choice = raw_input("[quit,check,pay]What do you want to buy?:").strip()
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice > -1:
shopping_cart.append(product_list[user_choice])
print("\033[32;1mJust added [%s,%s] in to your shopping cart\033[0m" %(product_list[user_choice]))
else:
print("\033[31;1mProduct [%s] is not exist!\033[0m")
elif user_choice == "check":
total_price = 0
print("\033[34;1mYou have bought below products...:\033[0m")
for index,p in enumerate(shopping_cart):
print(index,p)
total_price += p[1]
print("Total Price:\033[31;1m[%s]\033[0m" % total_price)
elif user_choice == "pay":
total_price = 0
print("\033[34;1mYou have bought below products...:\033[0m")
for index,p in enumerate(shopping_cart):
print(index,p)
total_price += p[1]
print("Total Price:\033[31;1m[%s]\033[0m" % total_price)
pay_confirm = raw_input("\033[31;1mGoing to pay?\033[0m").strip()
if pay_confirm == "y":
money_left = user_asset - total_price
if money_left > 0:
paid_list += shopping_cart
shopping_cart = []
user_asset = money_left
print("\033[31;1mYou have just paid[%s], your current balance is [%s]\033[0m" %(total_price,money_left))
go_confirm = raw_input("press any key to continue shopping!").strip() else:
print("\033[31;1mYour current balance is [%s], still need [%s] to pay the whole deal!\033[0m" %(user_asset,total_price-user_asset)) elif user_choice == "quit":
if shopping_cart:
print("You still have some product haven't paid yet!")
else:
print("Thanks for comming!")
break_flag = True
python基础:三层循环的更多相关文章
- 第五篇:python基础之循环结构以及列表
python基础之循环结构以及列表 python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...
- Python 基础 while 循环
Python 基础 while 循环 while 循环 在生活中,我们遇到过循环的事情吧?比如循环听歌.在程序中,也是存才的,这就是流程控制语句 while 基本循环 while 条件: # 循环体 ...
- python基础之循环结构以及列表
python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...
- python基础(六)循环
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 循环用于重复执行一些程序块.从上一讲的选择结构,我们已经看到了如何用缩进来表示程序 ...
- python基础之循环
一.while循环 如果条件成立(true),重复执行相同操作,条件不符合,跳出循环 while 循环条件: 循环操作 (1)while循环示例 例:输入王晓明5门课程的考试成绩,计算平均成绩 i ...
- python基础之循环语句
一.if条件语句: 语法: 1.if单分支(单重条件判断) if expression: expr_true_suite 注释:expession为真执行代码expr_true_suite if单分支 ...
- python基础之循环与迭代器
循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += ...
- Python基础->for循环、字符串以及元组
python流程控制>for循环.字符串以及元组 学习有关序列的思想.序列:一组有顺序的东西.所有的序列都是由元素组成的,序列中的元素位置是从0开始编号的,最后一个元素的位置是它长度减一. fo ...
- Python 基础06 循环
循环用于重复执行一些程序块.从上一讲的选择结构,我们已经看到了如何用缩进来表示程序块的隶属关系. 循环也会用到类似的写法. for 循环 for 循环需要预先设定好循环的次数(n) 然后执行隶属于fo ...
随机推荐
- 因为改 UOM conversion 导致库存数量和財务上的数据错误
轻易改变 UOM conversion 会导致库存数量混乱, 也会造成財务上的数据错误. 我们这里做一个 case 来详细分析一下. 1. 開始 Carton 和 Each 的比例是 1 : 1. 2 ...
- GPS原始经纬度转百度经纬度
protected void runTest() throws Throwable { try { BaiduLocation bl = new BaiduLocation(); bl.gpsx = ...
- WinForm中DefWndProc、WndProc与IMessageFilter的区别
这篇文章主要介绍了WinForm中DefWndProc.WndProc与IMessageFilter的区别,较为详细的分析了WinForm的消息处理机制,需要的朋友可以参考下 一般来说,Win ...
- C语言中将数字转换为字符串的方法
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio. h># ...
- C++-copy constructor、copy-assignment operator、destructor
本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...
- 【Mood-11】值得学习的国内外Android开发者信息
国内 Android 开发者信息: 昵称 GitHub 博客 介绍 罗升阳 Luoshengyang@csdn Android 源码分析 邓凡平 innost@csdn 阿拉神农 魏祝林 ...
- javascript 编程技巧
1.巧用判断: 在js中,NaN,undefined,Null,0,"" 在转换为bool的时候,是false,所以,可以这样写. if(!obj) {} 表示一个对象如果为fal ...
- 关于JSON的总结
本文总结自百度百科 JSON 语法规则 JSON 语法是 JavaScript 对象表示语法的子集. 数据在键值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 JSON 名称/值对 JSON 数 ...
- 转:关于视频H264编解码的应用实现
转:http://blog.csdn.net/scalerzhangjie/article/details/8273410 项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时 ...
- Oracle数据库作业-6 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 select tname,prof from teacher where depart = '计算机系' and prof not in ( select prof from teacher where depart 。
29.查询选修编号为"3-105"课程且成绩至少高于选修编号为"3-245"的同学的Cno.Sno和Degree,并按Degree从高到低次序排序. selec ...