python 基础之while无限循环
用户登录程序
username = "chenxi"
passwed = "testki"
counter = 0
while counter < 3: # 测试3次
user = input("输入用户名:")
passw = input("输入密码:")
if user == username and passw == passwed :
print("登录成功")
break #退出
else:
print("重新输入")
counter += 1
测试-1
D:\python\python.exe D:/untitled/dir/for.py
输入用户名:bhghjb
输入密码:njbmnbm
重新输入
输入用户名:bhbjb
输入密码:nnbnbm
重新输入
输入用户名:nnbmnb
输入密码:jhjh
重新输入 Process finished with exit code 0
测试-2
D:\python\python.exe D:/untitled/dir/for.py
输入用户名:chenxi
输入密码:testki
登录成功
打印0-9,小于5不打印
for i in range(10):
if i < 5 :
continue # 结束本次循环
print(i)
测试
D:\python\python.exe D:/untitled/dir/for.py
5
6
7
8
9
打印双层循环
for i in range(10):
print ("chenxi:",i)
for j in range(10):
print(j)
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxi: 0
0
1
2
3
4
5
6
7
8
9
chenxi: 1
0
1
2
3
4
5
6
7
8
9
chenxi: 2
0
1
2
3
4
5
6
7
8
9
chenxi: 3
0
1
2
3
4
5
6
7
8
9
chenxi: 4
0
1
2
3
4
5
6
7
8
9
chenxi: 5
0
1
2
3
4
5
6
7
8
9
chenxi: 6
0
1
2
3
4
5
6
7
8
9
chenxi: 7
0
1
2
3
4
5
6
7
8
9
chenxi: 8
0
1
2
3
4
5
6
7
8
9
chenxi: 9
0
1
2
3
4
5
6
7
8
9 Process finished with exit code 0
i小于5不循环
for i in range(10):
if i < 5 :
continue # 结束本次循环
print ("chenxi:",i)
for j in range(10):
print(j)
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxi: 5
0
1
2
3
4
5
6
7
8
9
chenxi: 6
0
1
2
3
4
5
6
7
8
9
chenxi: 7
0
1
2
3
4
5
6
7
8
9
chenxi: 8
0
1
2
3
4
5
6
7
8
9
chenxi: 9
0
1
2
3
4
5
6
7
8
9 Process finished with exit code 0
利用break当j=6时跳出本次循环体
for i in range(10):
if i < 5 :
continue # 结束本次循环
print ("chenxi:",i)
for j in range(10):
print(j)
if j == 6 :
break #当j=6时跳出循环体
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxi: 5
0
1
2
3
4
5
6
chenxi: 6
0
1
2
3
4
5
6
chenxi: 7
0
1
2
3
4
5
6
chenxi: 8
0
1
2
3
4
5
6
chenxi: 9
0
1
2
3
4
5
6 Process finished with exit code 0
利用标志物位跳出多层循环
# 小于5 不打印
exit_flag = False #设置exit_flag初始值
for i in range(10):
if i < 5 :
continue # 结束本次循环
print ("chenxi:",i)
for j in range(10):
print(j)
if j == 6 :
exit_flag = True# 当j = 6 时;修改exit_flag变量值为True
break #当j=6时跳出循环体
if exit_flag: #判断exit_flag=True时,跳出第二层循环体
break
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxi: 5
0
1
2
3
4
5
6
python 基础之while无限循环的更多相关文章
- 『Python基础-7』for循环 & while循环
『Python基础-7』for循环 & while循环 目录: 循环语句 for循环 while循环 循环的控制语句: break,continue,pass for...else 和 whi ...
- Python基础之条件和循环
阅读目录 一.if语句 1.1功能 1.2语法 1.2.1:单分支,单重条件判断 1.2.2:单分支,多重条件判断 1.2.3:if + else 1.2.4:多分支if + elif +else 1 ...
- Python基础(2)——循环和分支[xiaoshun]
一.瞎扯 世界上一切的系统都可以被'分支'表示.循环也是分支,只不过又重复之前的'分支'选择罢了.程序如人生,每一次的'分支',每一次的选择,都会有不同的结果: 有的选择止步不前,无限循环: 有的选择 ...
- Python基础知识:while循环
1.在循环中使用continue输出1-10之间的奇数 num=0 while num <10: num += 1 if num %2 == 0: #--%--运算符,相除返回余数 contin ...
- Python基础数据类型与for循环
数据类型:int,bool,str,list, tuple元组,dict字典. 1.数字:12,3,4 在使用print打印数字时,在终端界面中无法判断出打印的是什么类型,当我们需要知道一个值是什么类 ...
- Python基础7- 流程控制之循环
循环: 把一段代码重复性的执行N次,直到满足某个条件为止. 为了在合适的时候,停止重复执行,需要让程序出现满足停止循环的条件.Python中有三种循环(实质只有两种): while循环 for循环 嵌 ...
- Python基础(条件判断和循环) if elif else for while break continue;
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...
- Python之路,Day2 - Python基础,列表,循环
1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = &quo ...
- Python基础 条件判断和循环
pyhton if 语句 if 语句后接表达式,然后用: 表示代码块. age = 20 if age >= 18: print 'your age is', age print 'adult' ...
随机推荐
- Open-source Tutorial - NLog
1. Installing NLog 使用 NuGet 程序包管理器安装 NLog.如何使用 NuGet? 遇到问题:我的项目是 .Net Framework 4.0 平台的,虽然 NLog 说明中是 ...
- ]Linq to EF 增删改查
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- php curl采集,服务器gzip压缩返回数据怎么办
一般服务器不会胡乱返回gzip压缩的数据,一般是客户端请求的头部里包含你浏览器能接受的压缩方式, Accept-Encoding:gzip,deflate,sdch 这里是gzip .deflat ...
- 洛谷P2867 [USACO06NOV]大广场Big Square
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...
- 洛谷P3327 [SDOI2015]约数个数和(莫比乌斯反演)
传送门 公式太长了……我就直接抄一下这位大佬好了……实在懒得打了 首先据说$d(ij)$有个性质$$d(ij)=\sum_{x|i}\sum_{y|j}[gcd(x,y)=1]$$ 我们所求的答案为$ ...
- RabbiMQ原理与SpringBoot使用
RabbiMQ介绍 具体代码可参考我的github:https://github.com/UniqueDong/springboot-study 一.使用场景 RabbitMQ是一个消息中间件,所以最 ...
- 初次学习DropWizard框架——解决maven打包时出现没有主清单属性的问题
笔者因为公司的项目需要,开始接触DropWizard框架,照着官网https://www.dropwizard.io/0.9.2/docs/getting-started.html撸了一遍. 工具为I ...
- git push error: ! [rejected] failed to push some refs to . . .
报错情况: 报错原因:远程库与本地库不一致造成的,需要把远程库同步到本地库! 解决办法: git pull --rebase origin master 这条指令是将远程库中的更新合并到本地库,--r ...
- 字符串split函数
https://www.cnblogs.com/hjhsysu/p/5700347.html 函数:split() Python中有split()和os.path.split()两个函数,具体作用如下 ...