循环语句作业与循环补充(continue与break)
2024-10-08 03:00:35
作业:
1.使用while循环输入1 2 3 4 5 6 8 9 10(没有7)
n = 1
while n < 11:
if n == 7:
pass
else:
print(n)
n = n + 1
print('结束')
2.求1-100所有数的和
n = 1
s = 0
while n < 101:
s = s + n
n = n + 1
print(s)
3.输出1-100的所有奇数
n = 1
while n <101:
print(n)
n = n + 2
print('结束')
n = 1
while n <101:
temp = n % 2
if temp == 0:
pass
else:
print(n)
n = n + 2
print('结束')
4.输出1-100的所有偶数
n = 1
while n < 101:
temp = n % 2
if temp == 0:
print(n)
else:
pass
n = n + 1
print('结束')
5.求1-2+3-4+5...99的所有的数的和
n = 1
s = 0
while n < 101:
temp = n % 2
if temp == 0:
s = s - n
else:
s = s + n
n = n + 1
print(n)
6.用户登录(三次机会重试)
count = 0
while count < 3:
user = input('请输入用户名')
pwd = input('请输入密码')
if user == 'alex' and pwd == '':
print('欢迎登录')
break
else:
print('用户名或密码错误')
count = count + 1
注意:1.如果if语句里面加and,只需要在最后输入一个冒号:
2.IF下面的代码块注意空格一致
7.补充:a. while else
count =
while count < :
print(count)
count = count +
else:
print('else')
b.continue(终止当前循环开始下一次循环)
count =
while count < :
if count == :
count = count +
continue
print(count)
count = count +
注意:如果符合条件那么continue下面的代码都不再执行!
c.break(终止所有循环直接跳出)
count = 0
while count < 10:
count = count + 1
print(count)
break
print('haha')
print('hehe')
8.死循环:
注意最后print的是否空格区别。
count = 0
while count < 10:
print('继续运行')
count = count + 1
print('结束运行')
最新文章
- PPPoE(以太网上的点对点协议)
- Java中如何解决double和float精度不准的问题
- p188习题2
- Hibernate个人总结
- 2014年度辛星html教程夏季版第五节
- c语言学习之基础知识点介绍(二十):预处理指令
- 访问的是A网址,但是跳转B网址的内容,地址栏还是A网址
- c++中多态性、dynamic_cast、父类指针、父类对象、子类指针、子类对象
- 关于GROUP BY的应用
- 《算法》C/C++ 图形处理
- ListView 无 DataSource 依然用 DataPager 翻页
- python之模块、包的导入过程和开发规范
- mongoDB3.0版本使用express读取数据
- thinkphp微信浏览器内拉起微信支付
- Spring data Jpa,Mybatis,读写锁,@Lock 使用
- linux_shell使用
- Spring 配置文件
- Lagrange 乘子法求最优解
- PythonCharm 配置本地反向代理激活
- 照着官网来安装openstack pike之创建并启动instance
热门文章
- 洛谷1758 BZOJ1566 管道取珠题解
- poj 1655 Balancing Act 求树的重心【树形dp】
- vue页面内监听路由变化
- 数组工具类 Day07
- QT 开发ros gui过程中遇到:error: catkin_package() include dir &#39;include&#39; does not exist relative to &#39;/home/jun/catkin_ws/src/qt_ros_test&#39; /opt/ros/kinetic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_p
- js中setInterval与setTimeout用法 实现实时刷新每秒刷新
- UISearchDisplayController “No Results“ cancel修改
- hdu 3873 Invade the Mars(有限制的最短路 spfa+容器)
- 2016国产开源软件Top100(Q1)
- SuperSocket 服务器管理器 (ServerManager)