一、while循环

  while 条件:

    条件为真执行的语句

  esle:

    条件为假执行的语句

  

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
count = 0
while count < 100:
print("Count:",count)
count += 1

  猜年龄升级版

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
age_of_MrWang = 48
count = 0
while count < 3:
guess_age = int(input("Enter the age of Mr Wang:"))
if guess_age == age_of_MrWang:
print("Yes,you got it!")
break
elif guess_age < age_of_MrWang:
print("Think bigger!")
else:
print("Think smaller!")
count += 1
# if count == 3:
# print("You have tried too many times...Fuck off!")
else:
print("You have tried too many times...Fuck off!")

二、For循环

  for i in range (xx):

    语句

  else:

    上面循环里的语句正常走完了后,才执行这里的语句

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
for i in range(10):
print("loop",i)

  再次优化一下猜年龄小程序

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
age_of_MrWang = 48
for i in range (3):
guess_age = int(input("Enter the age of Mr Wang:"))
if guess_age == age_of_MrWang:
print("Yes,you got it!")
break
elif guess_age < age_of_MrWang:
print("Think bigger!")
else:
print("Think smaller!")
else:
print("You have tried too many times...Fuck off!")

三、for循环步长问题

  range里可以设置,默认是1

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
for i in range(0,10,2): #步长是2
print("loop",i)

四、猜年龄小程序加入继续玩模式

  思路:猜了三次还没有猜对,提示是否继续猜,如果按n就退出程序,其他键就继续猜。同时判断输入是否为数字

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
age_of_MrWang = 48
count = 0 #计数器
while count < 3:
guess_age = input("Enter the age of Mr Wang:")
if guess_age.isdigit(): #输入了数字
guess_age = int(guess_age)
if guess_age == age_of_MrWang:
print("Yes,you got it!")
break #猜对了,退出while循环
elif guess_age < age_of_MrWang:
print("Think bigger!")
else:
print("Think smaller!")
count += 1
if count == 3:
continue_confirm = input("Do you want to keey guessing?")
if continue_confirm != "n":
count = 0 # 计数器清零
else:
print("Please enter a number!")

  

五、break与continue

  continue:跳出本次循环(或者说:结束当前循环),继续下一次循环

  break:结束整个循环

  

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
for i in range(0,10):
if i < 5:
print("loop",i)
else:
continue
print("The variable i loops once")

  

六、for循环嵌套

  注意break的使用

  

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
for i in range(10):
print("-------",i)
for j in range (10):
print(j)
if j > 5:
break #结束当前循环(j),继续下一次循环

  

Day1:循环语句(While,For)的更多相关文章

  1. 【python之路4】循环语句之while

    1.while 循环语句 #!/usr/bin/env python # -*- coding:utf-8 -*- import time bol = True while bol: print '1 ...

  2. python之最强王者(3)——变量,条件、循环语句

    1.Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的 ...

  3. #9.5课堂JS总结#循环语句、函数

    一.循环语句 1.for循环 下面是 for 循环的语法: for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块) ...

  4. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  5. 【java开发】分支语句、循环语句学习

    一.Java分支语句类型 if-else 语句 switch 关于if-esle语句可以拆分为三种 if语句 if(条件){语句块;} if-else语句if(条件语句){语句块;} if-else ...

  6. python3循环语句while

    Python的循环语句有for和while语句,这里讲while语句. Python中while语句的一般形式: while 条件判断 : 语句 需要注意冒号和缩进.另外,注意Python中没有do. ...

  7. 20.SqlServer中if跟循环语句

    --if语句declare @i int begin print @i end else --循环语句 declare @i int begin insert into grade(classname ...

  8. Python学习【第五篇】循环语句

    Python循环语句 接下来将介绍Python的循环语句,程序在一般情况下是按顺序执行的. 编程语言提供了各种控制结构,允许更复杂的执行路径. 循环语句允许我们执行一个语句或语句组多次. Python ...

  9. iOS -Swift 3.0 -for(循环语句用法)

    // // ViewController.swift // Swift-循环语句 // // Created by luorende on 16/12/08. // Copyright © 2016年 ...

  10. Python--While循环语句

    Python While循环语句 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句 ...

随机推荐

  1. hdu1856 More is better (并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  2. 解决 php7 cli 模式下中文乱码的两中方法

    解决 php7 cli 模式下中文乱码的两中方法1. 给PHP文件开头加上 exec('chcp 936'); 然后把该文件以 ANSI 格式编码2. 在 php.ini 中设置 default_ch ...

  3. android 图片特效处理之光照效果

    这篇将讲到图片特效处理的光照效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理:图片上面的像素点按照给定圆心,按照圆半径的变化,像素点的RGB值分别加上相应的值作为当前点的RGB值. 例: ...

  4. 面向对象(OO)基础

    面向对象,英文Object-Oriented,简称OO),另外相关的还有:面向对象分析(Object-Oriented Analysis,OOA) 面向对象设计(Object-Oriented Des ...

  5. Spark Tachyon编译部署(含单机和集群模式安装)

    Tachyon编译部署 编译Tachyon 单机部署Tachyon 集群模式部署Tachyon 1.Tachyon编译部署 Tachyon目前的最新发布版为0.7.1,其官方网址为http://tac ...

  6. How to anti-Obfuscated code

    Author:jin can zhu from China Source:http://blog.csdn.net/clever101 Now many software makers have us ...

  7. Android java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses

    项目开发快到尾声,突然发现之前一个模块莫名其妙的奔溃了,我的内心也是奔溃的.以前一直都是好好的,也没去动过它,为啥会出现这样的问题呢? 下面我会根据自己的理解来看待问题 android是怎么根据id查 ...

  8. 转 SQL集合函数中利用case when then 技巧

    SQL集合函数中利用case when then 技巧 我们都知道SQL中适用case when then来转化数据库中的信息 比如  select (case sex when 0 then '男' ...

  9. Codefroces B. New Skateboard

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. iframe自适应高度解决方法 .

    <div id="leftBar"> <iframe name="tag" src="_iframe.html" styl ...