Board logo

标题: while循环语句 [打印本页]

作者: admin    时间: 2019-9-23 14:46     标题: while循环语句

例子一: #coding=utf-8 Key_1 = 0 while Key_1 < 3: print('当前数字是:',Key_1) Key_1 = Key_1 + 1 print(f"一共执行了 {Key_1} 次。") 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 当前数字是: 0 当前数字是: 1 当前数字是: 2 一共执行了 3 次。 ###### 例子二: #coding=utf-8 Key_1 = 0 while Key_1 < 3: print('当前数字是:',Key_1,",小于3") Key_1 = Key_1 + 1 else: print("-" * 10) print('当前数字是:',Key_1,",大于或等于3") print('循环结束') print("-" * 10) print('循环外') print('当前数字是:',Key_1) 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 当前数字是: 0 ,小于3 当前数字是: 1 ,小于3 当前数字是: 2 ,小于3 ---------- 当前数字是: 3 ,大于或等于3 循环结束 ---------- 循环外 当前数字是: 3 ###### 例子三: #coding=utf-8 Key_1 = 0 fields_1 = ['a','b','c','d','e'] #列表 while Key_1 < len(fields_1): print('当前字母是:',fields_1[Key_1]) Key_1 = Key_1 + 1 print("-" * 10) print('循环外') print('当前字母是:',fields_1[Key_1 - 1]) print(f"这个列表的元素数量有 {Key_1} 个。") 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 当前字母是: a 当前字母是: b 当前字母是: c 当前字母是: d 当前字母是: e ---------- 循环外 当前字母是: e 这个列表的元素数量有 5 个。 ###### 例子四: #coding=utf-8 Key_1 = 0 fields_1 = ('a','b','c','d','e','f') #元组 while Key_1 < len(fields_1): print('当前字母是:',fields_1[Key_1]) Key_1 = Key_1 + 1 print("-" * 10) print('循环外') print('当前字母是:',fields_1[Key_1 - 1]) print(f"这个元组的元素数量有 {Key_1} 个。") 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 当前字母是: a 当前字母是: b 当前字母是: c 当前字母是: d 当前字母是: e 当前字母是: f ---------- 循环外 当前字母是: f 这个元组的元素数量有 6 个。 ###### 例子五: #coding=utf-8 fields_1 = ('小明','大海','Wu zhuohua.','Python') K_1 = "zhuohua" #关键字;区分英文字母大小写 Key_1 = 0 while Key_1 < len(fields_1): if K_1 in fields_1[Key_1]: #这里相当于模糊查询(in) print("找到匹配的用户名:",fields_1[Key_1]) break #只是结束整个while循环,不是结束程序 Key_1 = Key_1 + 1 else: print(f"没有找到匹配的用户名。") 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 找到匹配的用户名: Wu zhuohua. ###### 例子六: #coding=utf-8 fields_1 = ('小明','大海','Wu zhuohua.','Python') K_1 = "Zhuohua" #关键字;区分英文字母大小写 Key_1 = 0 while Key_1 < len(fields_1): if K_1 in fields_1[Key_1]: #这里相当于模糊查询(in) print("找到匹配的用户名:",fields_1[Key_1]) break #只是结束整个while循环,不是结束程序 Key_1 = Key_1 + 1 else: print(f"没有找到匹配的用户名。") 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 没有找到匹配的用户名。 ###### 例子七: #coding=utf-8 fields_1 = ['a','b','c','zhuohua','Python'] #列表 K_1 = "B" #关键字;区分英文字母大小写 K_2 = "zhuohua" #关键字; K_3 = "Python" #关键字; Key_1 = 0 while Key_1 < len(fields_1): if K_1 == fields_1[Key_1] or K_2 == fields_1[Key_1] or K_3 == fields_1[Key_1]: print(f"找到第一个对应的元素:{fields_1[Key_1]},查询就此结束。") break #只是结束整个while循环,不是结束程序 Key_1 = Key_1 + 1 else: print(f"没有找到对应的元素。") 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 找到第一个对应的元素:zhuohua,查询就此结束。 ###### 例子八: #coding=utf-8 Key_1 = 5 while Key_1 > 0: Key_1 = Key_1 - 1 print(Key_1) print("-" * 10) print('循环外') print(Key_1) 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 4 3 2 1 0 ---------- 循环外 0 ###### 例子九: #coding=utf-8 Key_1 = 5 while Key_1 > 0: Key_1 = Key_1 - 1 if Key_1 == 2 or Key_1 == 4: continue #跳过当前这次while循环,继续下一轮while循环 print(Key_1) print("-" * 10) print('循环外') print(Key_1) 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 3 1 0 ---------- 循环外 0 ###### 例子十: #coding=utf-8 fields_1 = ['小明','大海','Wu zhuohua.','Python'] Key_1 = len(fields_1) K_1 = "h" #关键字;区分英文字母大小写 K_2 = "明" #关键字; print(f"以下是不包含关键字'{K_1}'、'{K_2}'的元素:") while Key_1 > 0: Key_1 = Key_1 - 1 if K_1 in fields_1[Key_1] or K_2 in fields_1[Key_1]: #这里相当于模糊查询(in) continue #跳过当前这次while循环,继续下一轮while循环 print(fields_1[Key_1]) 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 以下是不包含关键字'h'、'明'的元素: 大海 ###### 例子十一: #coding=utf-8 fields_1 = ['小明','大海','Wu zhuohua.','Python'] Key_1 = len(fields_1) K_1 = "海" #关键字 K_2 = "zhuohua" #关键字;区分英文字母大小写 print(f"以下是包含关键字'{K_1}'或'{K_2}'的元素:") while Key_1 > 0: Key_1 = Key_1 - 1 if K_1 not in fields_1[Key_1] and K_2 not in fields_1[Key_1]: continue #跳过当前这次while循环,继续下一轮while循环 print(fields_1[Key_1]) 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 以下是包含关键字'海'或'zhuohua'的元素: Wu zhuohua. 大海 相关文章: Timer/sleep Python3制作猜字游戏 Python3制作音乐播放器 字典 for循环语句 字符串的截取




欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2