三层循环基本演示:

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

  

最新文章

  1. PHP-&gt;利用GD库新建图像
  2. 前端开发--css属性书写顺序
  3. MySQL字符集乱码
  4. WebAPI GET和POST请求的几种方式
  5. U盘安装win7+CentOS7双系统
  6. Hibernate 核心接口和工作机制
  7. MPLS LDP随堂笔记1
  8. 什么是git?window下安装git
  9. Spring IOC容器分析(1) -- BeanFactory
  10. Visual Studio Code+phpstudy(WampServer、LNMP...)为小白打造PHP开发环境
  11. ES6躬行记(21)——类的继承
  12. Fundebug前端JavaScript插件更新至1.7.1,拆分录屏代码,还原部分Script error.
  13. python3 独立环境 virtualenv &amp; conda
  14. windows 下设置nginx负载均衡
  15. poj 4468Spy(kmp算法)
  16. JS - 兼容到ie7的自定义样式的滚动条封装
  17. Git+Jenkins+FileGee 发布php应用
  18. SPA (单页应用程序)
  19. DevExpress v18.1新版亮点——ASP.NET篇(三)
  20. Java面向对象技术

热门文章

  1. 一步步学Mybatis-告别繁琐的配置之Mybatis配置文件生成工具 (7)
  2. 引入第三方库错误Undefined symbols for architecture i386: _OBJC_CLASS_$的解决方案
  3. 索引节点inode
  4. (文件描述符0、1、2),(stdin、stdout、stderr),(终端设备)这三者之间的关系???
  5. 实例讲解Linux系统中硬链接与软链接的创建
  6. LMAX Disruptor – High Performance, Low Latency and Simple Too 转载
  7. LeetCode1 Two Sum
  8. [Arduino] 外部中断
  9. 12. Android框架和工具之 StringUtils(字符串操作)
  10. Oracle 常用的SQL语法和数据对象