python提供了for循环和while循环以及嵌套循环(在python中没有do..while循环)

while 循环语法:

while 判断条件:
执行语句......

实际案例:

numbers=[,,,,,]
even=[];
odd=[];
while len(numbers)>:
number=numbers.pop();
if(number%==):
even.append(number)
else:
odd.append(number); 结果:
numbers=[];
even=[,,];
odd=[,,]

循环使用else语句 实例:

count =
while count < :
print count, " is less than 5"
count = count +
else:
print count, " is not less than 5“ 结果;
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
 

做一些简单的小游戏练习

猜大小的游戏

import random
s = int(random.uniform(,))
#print(s)
m = int(input('输入整数:'))
while m != s:
if m > s:
print('大了')
m = int(input('输入整数:'))
if m < s:
print('小了')
m = int(input('输入整数:'))
if m == s:
print('OK')
break;

猜拳小游戏

import random
while :
s = int(random.randint(, ))
if s == :
ind = "石头"
elif s == :
ind = "剪子"
elif s == :
ind = "布"
m = raw_input('输入 石头、剪子、布,输入"end"结束游戏:')
blist = ['石头', "剪子", "布"]
if (m not in blist) and (m != 'end'):
print "输入错误,请重新输入!"
elif (m not in blist) and (m == 'end'):
print "\n游戏退出中..."
break
elif m == ind :
print "电脑出了: " + ind + ",平局!"
elif (m == '石头' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石头'):
print "电脑出了: " + ind +",你赢了!"
elif (m == '石头' and ind =='布') or (m == '剪子' and ind =='石头') or (m == '布' and ind =='剪子'):
print "电脑出了: " + ind +",你输了!"

摇筛子游戏

import random
import sys
import time result = []
while True:
result.append(int(random.uniform(,)))
result.append(int(random.uniform(,)))
result.append(int(random.uniform(,)))
print result
count =
index =
pointStr = ""
while index >= :
currPoint = result[index]
count += currPoint
index -=
pointStr += " "
pointStr += str(currPoint)
if count <= :
sys.stdout.write(pointStr + " -> " + "小" + "\n")
time.sleep( ) # 睡眠一秒
else:
sys.stdout.write(pointStr + " -> " + "大" + "\n")
time.sleep( ) # 睡眠一秒
result = []

python for 循环可以遍历任何序列的项目,如一个列表或者一个字符串。

for循环的语法:

for iterating_var in sequence
statements(s)

简单案例

for letter in 'Python':     # 第一个实例
print '当前字母 :', letter fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit

通过序列索引迭代:将列表编程序列索引

fruits=["banana","apple","mango"]
for index in range(len(fruits)):
print(index)
print("当前水果:"+fruits[index])

循环使用else语句

在python中,for...else 表示这样的意思,for中的语句和普通的没有区别,else中的语句会在循环正常执行完(即for不是通过break跳出而中断的) 的情况下执行,while...else也是一样

for num in range(,):
for i in range(,num):
if(num%i)==:
j=num/i
print("%d 等于 %d * %d" %(num,i,j))
break
else:
print(str(num) +“是一个质数”)

使用内置enumerate 函数进行遍历  案例:

sequence=[,,,,,,]
for i ,j in enumerate(sequence):
print(i,j)

python 之 循环语句的更多相关文章

  1. Python for 循环语句

    Python for 循环语句 Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for循环的语法格式如下: for iterating_var in sequenc ...

  2. python:while循环语句及练习题

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

  3. python的循环语句

    python的循环语句有两种:for 和 while,for循环是对可迭代对象进行迭代并处理,因此for的对象是一个可以迭代的对象,而while循环的条件则是一个布尔值可以是一个返回布尔值的表达式. ...

  4. Python for循环语句

    Python for 循环语句:遍历任何序列的项目,可以是字符串.列表.元组.字典.集合对象. 流程图: 第一种: ''' for 迭代对象 in 序列: 代码块(一行语句或多行代码) ''' 第二种 ...

  5. python基础-循环语句(5)

    一.循环语句介绍 一般情况下,需要多次重复执行的代码,都可以用循环的方式来完成 循环不是必须要使用的,但是为了提高代码的重复使用率,所以有经验的开发者都会采用循环 二.常见的循环形式 while循环 ...

  6. Python学习-7.Python的循环语句-for语句

    Python中循环可以使用for语句来实现 list = ['Tom','Lucy','Mary'] for name in list: print(name) 则将会依次输出Tom Lucy Mar ...

  7. Python从零开始——循环语句

    一:Python循环语句知识概览 二:while循环 三:for遍历 四:循环控制

  8. Python(循环语句与数据类型)

    循环语句 对于python来说 基本上循环用的两个 wile 跟静态语言相似 下来是for循环 这个就跟静态语言大大不同了 wile 条件:–>while 循环也就是 当条件为真的时候会一直循环 ...

  9. python基础-循环语句while

    循环语句:while\for\嵌套 循环控制语句:break\continue break:跳出整个循环,不会再继续循环下去 continue:跳出本次循环,继续下一次循环 while循环: coun ...

随机推荐

  1. html5-内联框架

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. Robot - 1. robot framework环境搭建

    Fom:https://www.cnblogs.com/puresoul/p/3854963.html 一. robot framework环境搭建: 官网:http://robotframework ...

  3. Java集合-----List详解

    List中的元素是有序排列的而且可重复 1.LinkedList LinkedList是非线程安全的,底层是基于双向链表实现的       LinkedList常用方法:     toArray()  ...

  4. MVC请求管道

    下面是请求管道中的19个事件. (1)BeginRequest: 开始处理请求 (2)AuthenticateRequest授权验证请求,获取用户授权信息 (3):PostAuthenticateRe ...

  5. 集合运算—union(并集)、intersect(交集)和except(差集)

    一.集合运算的基本格式是: 集合查询1 <集合运算> 集合查询2 [order by ...] 二.集合运算符是对两个集合操作的,两个集合必须具有相同的列数,列具有相同的数据类型(至少能隐 ...

  6. byte以及UTF-8的转码规则

    https://www.cnblogs.com/hell8088/p/9184336.html 多年来闲麻烦,只记录笔记,不曾编写BLOG,本文为原创,如需转载请标明出处 废话不说,直奔主题 asci ...

  7. byte & 0xff char 转换

    https://blog.csdn.net/lixingtao0520/article/details/75450883 版权声明:本文为博主原创文章,转载请注明作者与出处,http://blog.c ...

  8. printf("loops %u / %u%c[K\n", loops + 1, opts->loops, 27); printf("%cM", 27);

    serialcheck.c中的一段代码一直弄不明白: do { status = stress_test_uart_once(opts, fd, data, data_len); memset(opt ...

  9. JAVA获取不同操作系统的分隔符等参数

    import java.util.Properties; public class SeparatorUtils { /* system properties to get separators */ ...

  10. sping的quartz设置定时任务

    除了spring相关的jar包之外,还需要引入quartz-all-1.8.6.jar 下载地址:http://www.quartz-scheduler.org/downloads/ spring配置 ...