一、 从第3层循环直接跳出所有循环

break_flag = False
count = 0
while break_flag == False:
print("-第一层")
while break_flag == False:
print("第二层")
while break_flag == False:
count += 1
if count > 10:
break_flag = True
print("第三层")
print("keep going...")

with break_flag

# break_flag = False
count = 0
while count < 3:
print("-第一层")
while count < 3:
print("第二层")
while count <= 10:
count += 1
# if count > 10:
# break_flag = True
print("第三层")
print("keep going...")

without break_flag

二、 购物车程序

goods_list = [['Iphone7',5800],['Coffee',30],['Book',99],['Bike',199],['Vivi X9',2499]]     #商品列表
shopping_cart = [] #用户购物车列表
salary = int(input('input your salary:')) #用户薪水
m = salary
k = 0 while True:
index = 0
for goods in goods_list: #打印商品列表
print(index,goods)
index += 1
choice = input('>>>:').strip()
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(goods_list):
goods = goods_list[choice]
if goods[1] <= salary: #判断用户是否带了足够的钱来支付所选商品
shopping_cart.append(goods)
salary -= goods[1]
print('Add goods '+str(goods[0])+' into shopping cart! Your current balance:'+str(salary))
else:
print('No enough money!The price is:'+str(goods[1])+'! Need more:'+str(goods[1]-salary))
else:
print('Have no this goods!')
elif choice == 'q':
print('----------Your shopping cart----------')
print('ID goods quantity price total')
for i in range(len(goods_list)):
j = shopping_cart.count(goods_list[i])
if j > 0 :
k += 1
print(k,'\t',goods_list[i][0],'\t',j,'\t\t',goods_list[i][1],'\t',j * goods_list[i][1]) print('Total price is:',m - salary)
break
else:
print('Have no this goods')

购物车程序

goods_list = [['Iphone7',5800],['Coffee',30],['Book',99],['Bike',199],['Vivi X9',2499]]  # 商品列表
shopping_cart = {} # 用户购物车列表从列表变为字典
salary = int(input('input your salary:')) # 用户薪水
# m = salary #不需要此项
# k = 0 #用户购物车商品打印ID,移动位置至打印循环外 while True:
index = 0
for goods in goods_list: # 打印商品列表
print(index, goods)
index += 1
choice = input('>>>:').strip()
if choice.isdigit(): # 判断是否为数字
choice = int(choice)
if choice >= 0 and choice < len(goods_list): # 商品存在
goods = goods_list[choice]
if goods[1] <= salary: # 判断用户是否带了足够的钱来支付所选商品
if goods[0] in shopping_cart: # 之前买过
shopping_cart[goods[0]][1] += 1 # 购物数量加1
else:
shopping_cart[goods[0]] = [goods[1], 1] # 创建一条新增商品的购买记录
salary -= goods[1] # 扣钱
print('Add goods ' + str(goods[0]) + ' into shopping cart! Your current balance:' + str(salary))
else:
print('No enough money!The price is:' + str(goods[1]) + '! Need more:' + str(goods[1] - salary))
else:
print('Have no this goods!')
elif choice == 'q':
id_counter = 1 # 初始化商品ID
total_cost = 0 # 初始化商品总花费
print('----------Your shopping cart----------')
print('ID goods quantity price total')
for i in shopping_cart:
print("%-5s%-10s%-12s%-10s%-s"
% (
id_counter, i, shopping_cart[i][1], shopping_cart[i][0],
shopping_cart[i][0] * shopping_cart[i][1]))
id_counter += 1 # 商品ID自加1
total_cost += shopping_cart[i][0] * shopping_cart[i][1] # 用户已购商品总花费
print('Total price is:', total_cost)
break
else:
print('Have no this goods')

购物车程序优化,使用字典

三、多级菜单

要求:

  1. 打印省、市、县三级菜单
  2. 可返回上一级(b,返回)
  3. 可随时退出程序(q,退出)
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
} exit_flag = False
current_layer = menu layers = [menu] while not exit_flag:
for k in current_layer:
print(k)
choice = input(">>:").strip()
if choice == "b":
current_layer = layers[-1]
#print("change to laster", current_layer)
layers.pop()
elif choice == 'q':
break
elif choice not in current_layer:
continue
else:
layers.append(current_layer)
current_layer = current_layer[choice]

四、用户登陆程序

需求:

  1. 最多允许用户尝试登陆3次
  2. 当同一用户名3次密码均不正确时,锁定该用户
 with open('account',encoding='utf8') as f_account, open('lockedlist', 'a+') as f_locked:
l = [] #定义用户名验证列表,存放黑名单数据
f_locked.seek(0) #"a+"模式打开后,文件位置位于末尾,要遍历文件内容,需要将指针移至文件起始位置
for locked_info in f_locked.readlines(): #遍历黑名单
l.append(locked_info.strip()) #将黑名单数据添加进列表,注意:需要将文件中的换行符脱掉 c = [] #定义用户登录名列表,存储用户尝试的登录名
count = 1 #登陆次数计数器
flag = True #登陆循环控制开关
while flag and count < 4:
user = input('Please input username:') #输入用户名
pwd = input('Please input password:') #输入用户密码
if user in l: #用户名在黑名单中
print("This user is in blacklist,can't log in!") #打印提示信息
continue
c.append(user)
for info in f_account: #用户名不在黑名单中,遍历用户登陆文件
user_name, user_pwd = info.strip().split(',') #将文件中的用户名和登陆密码赋值给判定变量
if user == user_name: #用户名符合
if pwd == user_pwd: #对应密码符合
print('Welcome %s' % user) #打印登陆成功信息
flag = False #登陆成功,跳出登陆程序
break
count += 1 #对应密码不符合,while循环计数器加1
break
count += 1 #用户名不符合,while循环计数器加1
break
if count == 4: #如果不同用户名密码输错3次,关闭程序
print('More than 3 times wrong!') #打印提示信息
if len(c) == 3 and c[0] == c[1] == c[2]: #如果相同用户名密码输错3次,将该用户名加入黑名单,限制登陆
print('This user has been locked!')
f_locked.write(user+'\n') #将错误3次的用户名写入黑名单,注意,加换行符

用户登陆程序

Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)的更多相关文章

  1. python中从内部循环直接跳出多层循环

    学习循环的时候碰到一道题,需要从内部循环中直接跳出所有循环,想了很久终于想到一种好办法(小白认知) 题目为:使用while循环输出100-50,从大到小,到50时,再循环输出0-50,从小到大. ex ...

  2. 关于微信小程序如何解决多层循环嵌套

    http://www.jianshu.com/p/87cdf985b2b9 附加:小程序里面一些功能示例 http://blog.csdn.net/column/details/13721.html

  3. python基础代码(猜年龄、从最内层跳出多层循环、简单的购物车程序)

    1.猜年龄 , 可以让用户最多猜三次! age = 55 i=0 while i<3: user_guess = int (input ("input your guess:" ...

  4. python练习-跳出多层循环和购物车

    跳出多层循环:三层循环,最里层直接跳出3层 在Python中,函数运行到return这一句就会停止,因此可以利用这一特性,将功能写成函数,终止多重循环 def work(): for i in ran ...

  5. python(3)- 循环语句:从最内层跳出多层循环

    跳出多层循环:三层循环,最里层直接跳出3层 方法一: 在Python中,函数运行到return这一句就会停止,因此可以利用这一特性,将功能写成函数,终止多重循环 def work(): #定义函数 f ...

  6. 微信小程序-列表渲染多层嵌套循环

    微信小程序-列表渲染多层嵌套循环 入门教程之列表渲染多层嵌套循环,目前官方的文档里,主要是一维数组列表渲染的案例,还是比较简单单一,给刚入门的童鞋还是无从入手的感觉. <view wx:for= ...

  7. Python基本小程序

    目录 Python基本小程序 一.筛选从1-100所有的奇数 二.筛选从0-100所有的偶数 三.求1-100之间所有的偶数和,奇数和 四.三个数由小到大输出 五.四个数字重复数字的三位数 Pytho ...

  8. 一个python爬虫小程序

    起因 深夜忽然想下载一点电子书来扩充一下kindle,就想起来python学得太浅,什么“装饰器”啊.“多线程”啊都没有学到. 想到廖雪峰大神的python教程很经典.很著名.就想找找有木有pdf版的 ...

  9. java的break跳出多层循环

    记得大一的时候,语言学的不好,碰到了需要跳出双层循环的时候,就没有了办法.因为老师讲了goto然后说不要用goto...  自己就一直感觉这种跳出多层循环的想法是不可取的(好蠢) 下面用java代码的 ...

随机推荐

  1. Ubuntu定时任务设置

    设置很简单,但如果误入歧途,会耽误很多时间 步骤如下: 1. 以root执行:vi /etc/crontab 2. 在文件最后添加cron配置(每天凌晨四点执行,并将日志输出到/data/cron.l ...

  2. [Java基础]-- Java GC 垃圾回收器的分类和优缺点

    https://blog.csdn.net/high2011/article/details/80177473?utm_source=blogxgwz2 参考:elasticsearch实战-使用G1 ...

  3. iOS开发ApplePay的介绍与实现

    1.Apple Pay的介绍 Apple Pay官方 1.1 Apple Pay概念 Apple Pay,简单来说, 就是一种移动支付方式.通过Touch ID/ Passcode,用户可使用存储在i ...

  4. mouseover和mouseenter闪烁的问题

    span标签绑定mouseover/mouseout事件,显示/隐藏一个信息框div 该div下没有任何子元素 悬停上去一直闪烁,改成mouseenter也没用. 照成的原因是:悬停上去信息框div盖 ...

  5. Python编程中报过的错

    一.TypeError: not all arguments converted during string formatting def max(*args): print('max2:%s' % ...

  6. myEclipse修改字体大小

  7. 转:JVM系列三:JVM参数设置、分析

    转自:http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html 不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断 ...

  8. ServletRequestLister

    1 知识点

  9. 入门系列之在Ubuntu上使用Netdata设置实时性能监控

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由小翼 发表于云+社区专栏 介绍 Netdata通过可扩展的Web仪表板提供准确的性能监控,可以显示Linux系统上的流程和服务.它监控 ...

  10. 周记2——ios的日期格式bug

    转眼又到了周末,转眼又要上班,转眼...大概这就是一眼万年的意思吧. 这周继续IM(即时聊天),项目用的是LayIM移动端改装的,仅仅“借用”了一个聊天窗口.由于是内嵌App的页面,自然少不了Andr ...