用户登录程序

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无限循环的更多相关文章

  1. 『Python基础-7』for循环 & while循环

    『Python基础-7』for循环 & while循环 目录: 循环语句 for循环 while循环 循环的控制语句: break,continue,pass for...else 和 whi ...

  2. Python基础之条件和循环

    阅读目录 一.if语句 1.1功能 1.2语法 1.2.1:单分支,单重条件判断 1.2.2:单分支,多重条件判断 1.2.3:if + else 1.2.4:多分支if + elif +else 1 ...

  3. Python基础(2)——循环和分支[xiaoshun]

    一.瞎扯 世界上一切的系统都可以被'分支'表示.循环也是分支,只不过又重复之前的'分支'选择罢了.程序如人生,每一次的'分支',每一次的选择,都会有不同的结果: 有的选择止步不前,无限循环: 有的选择 ...

  4. Python基础知识:while循环

    1.在循环中使用continue输出1-10之间的奇数 num=0 while num <10: num += 1 if num %2 == 0: #--%--运算符,相除返回余数 contin ...

  5. Python基础数据类型与for循环

    数据类型:int,bool,str,list, tuple元组,dict字典. 1.数字:12,3,4 在使用print打印数字时,在终端界面中无法判断出打印的是什么类型,当我们需要知道一个值是什么类 ...

  6. Python基础7- 流程控制之循环

    循环: 把一段代码重复性的执行N次,直到满足某个条件为止. 为了在合适的时候,停止重复执行,需要让程序出现满足停止循环的条件.Python中有三种循环(实质只有两种): while循环 for循环 嵌 ...

  7. Python基础(条件判断和循环) if elif else for while break continue;

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  8. Python之路,Day2 - Python基础,列表,循环

    1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = &quo ...

  9. Python基础 条件判断和循环

    pyhton if 语句 if 语句后接表达式,然后用: 表示代码块. age = 20 if age >= 18: print 'your age is', age print 'adult' ...

随机推荐

  1. CodeForces - 633B A Trivial Problem 数论-阶乘后缀0

    A Trivial Problem Mr. Santa asks all the great programmers of the world to solve a trivial problem. ...

  2. QDUOJ 河老师的新年礼物(尺取法)

    河老师的新年礼物 发布时间: 2017年1月1日 15:11   最后更新: 2017年1月1日 15:13   时间限制: 1000ms   内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...

  3. 使用的SQLServer版本不支持数据类型“datetime2“

    快速解决方法: 原因,在使用ado.net entity的时候,entity使用的数据库是sqlserver 2008, 但后来实际使用中使用的数据库是sqlserver 2005, 操作DateTi ...

  4. SCUT - 278 - 谜题#020 - 左偏树

    https://scut.online/p/278 第一次遇到不需要并查集的左偏树. #include<bits/stdc++.h> using namespace std; typede ...

  5. SCUT - 15 - 为美好的世界献上爆炎 - dfs

    https://scut.online/p/15 样例错了,按题目说的去做就AC了. 反向搜索使得最终比较strncmp的时候复杂度下降了很多(虽然应该可行性剪枝也少了) #include<bi ...

  6. redis win连接以及配置连接密码

    redis连接格式为 redis-cli -h host -p port -a password 但由于刚安装的redis是没有密码的 因此可以进行直接连接, cd转到redis目录里 redis-c ...

  7. 计算机视觉(CV)前沿国际国内期刊与会议

    计算机视觉(CV)前沿国际国内期刊与会议1.国际会议 2.国际期刊 3.国内期刊 4.神经网络 5.CV 6.数字图象 7.教育资源,大学 8.常见问题 1. 国际会议现在,国际上计算机视觉方面的三大 ...

  8. 洛谷P1044 栈(Catalan数)

    P1044 栈 题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要 ...

  9. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  10. python3 安装虚拟镜像

    virtualenvwrapper--提供了一系列命令使得和虚拟环境工作变得愉快很多,他把你所需要的虚拟环境都放在一个地方. 1.先安装virtualenv:pip install virtualen ...