循环
- 循环是一种控制语句块重复执行的结构
- while 适用于广度遍历
- for 开发中经常使用
while 循环
-
当一个条件保持真的时候while循环重复执行语句
-
while 循环一定要有结束条件,否则很容易进入死循环
-
while 循环的语法是:
while loop-contunuation-conndition:
Statement
In [ ]:
i = 0
while i<10:
print('hahaha')
i += 1
示例:
sum = 0
i = 1
while i <10:
sum = sum + i
i = i + 1
错误示例:
sum = 0
i = 1
while i <10:
sum = sum + i
i = i + 1
- 一旦进入死循环可按 Ctrl + c 停止
EP:
验证码
- 随机产生四个字母的验证码,如果正确,输出验证码正确。如果错误,产生新的验证码,用户重新输入。
- 验证码只能输入三次,如果三次都错,返回“别爬了,我们小网站没什么好爬的”
- 密码登录,如果三次错误,账号被锁定
In [1]:
import random
In [29]:
n = random.randint(65,122)
N = ""
i = 0
while 1:
if 91<=n<=96:
n = random.randint(65,122)
else:
N += chr(n)
n = random.randint(65,122)
i += 1
if i == 4:
break
print(N)
In [ ]:
count = 0
for i in range(1000):
a = random.randint(0,1000) / 1000
if 0<a<0.001
In [ ]:
尝试死循环
实例研究:猜数字
- 你将要编写一个能够随机生成一个0到10之间的且包括两者的数字程序,这个程序
- 提示用户连续地输入数字直到正确,且提示用户输入的数字是过高还是过低
使用哨兵值来控制循环
- 哨兵值来表明输入的结束
警告
for 循环
- Python的for 循环通过一个序列中的每个值来进行迭代
- range(a,b,k), a,b,k 必须为整数
- a: start
- b: end
- k: step
- 注意for 是循环一切可迭代对象,而不是只能使用range
In [9]:
for i in range(100):
print('Joker is a better man!')
. . .
In [14]:
a = 100
In [10]:
bb = 'JOker'
bb.__iter__()
Out[10]:
In [ ]:
In [16]:
c = [1,2,3]
c.__iter__
In [ ]:
{'key':'value'}.__iter__
(1,3,43).__iter__
{1,2,43}.__iter__
In [19]:
for i in range(5):
print(i)
在Python里面一切皆对象
EP:
In [2]:
i = 1
sum_ = 0
while sum_ < 10000:
sum_ += i
i += 1
print(sum_)
In [1]:
sum_ = 0
for i in range(1,10001):
sum_ += i
if sum_ > 10000:
break
print(sum_)
In [22]:
sum = 0
i = 0
while i < 1001:
sum = sum + i
i += 1
print(sum)
嵌套循环
- 一个循环可以嵌套另一个循环
- 每次循环外层时,内层循环都会被刷新重新完成循环
- 也就是说,大循环执行一次,小循环会全部执行一次
- 注意:
- 多层循环非常耗时
- 最多使用3层循环
In [ ]:
EP:
- 使用多层循环完成9X9乘法表
- 显示50以内所有的素数
关键字 break 和 continue
- break 跳出循环,终止循环
- continue 跳出此次循环,继续执行
In [19]:
for i in range(1,10):
for j in range(1,i+1):
print(j,'X',i,'=',i*j,end=' ')
print()
. . .
注意
Homework
- 1
In [4]:
zhengshu = 0
fushu = 0
sum_ = 0
cishu = 0
data = 1
def z():
global zhengshu
global fushu
global sum_
global cishu
global data
while data !=0 :
data = eval(input(">>"))
if data > 0:
zhengshu += 1
if data < 0:
fushu += 1
sum_ += data
if data != 0:
cishu += 1
print(zhengshu)
print(fushu)
print(sum_ / cishu)
z()
- 2
In [7]:
def f():
dorlla = 10000
for i in range(14):
dorlla = dorlla * 0.05 + dorlla
if i == 9:
print(dorlla)
print(dorlla)
f()
- 3
In [ ]:
zhengshu = 0
fushu = 0
sum_ = 0
cishu = 0
data = 1
def z():
global zhengshu
global fushu
global sum_
global cishu
global data
while data !=0 :
data = eval(input(">>"))
if data > 0:
zhengshu += 1
if data < 0:
fushu += 1
sum_ += data
if data != 0:
cishu += 1
print(zhengshu)
print(fushu)
print(sum_ / cishu)
z()
- 4
In [10]:
def f():
count = 0
for i in range(100,1001):
if i %5==0 and i % 6 == 0:
print(i,end=" ")
count += 1
if count % 10 == 0:
print()
f()
- 5
In [15]:
n = 0
while n**2 < 12000:
n += 1
print(n)
In [17]:
n = 0
while n**3 < 12000:
n += 1
print(n-1)
In [18]:
n = 0
while 1:
res = n ** 2
if res >= 12000:
break
else:
n += 1
print(n)
In [21]:
n = 0
while 1:
res = n ** 3
if res < 12000:
n += 1
else:
break
print(n-1)
In [ ]:
- 6
In [41]:
Money = 10000
lilv = 5
nian = 5
for i in range(24):
M = Money * lilv / 100
T = 12 * M * nian
print(lilv,'%','月利率',M,'总和',T)
if lilv == 5.25:
print('.....')
lilv += 1/8
- 7
In [1]:
res = 0
for i in range(50000,0,-1):
res += 1/i
print(res)
In [2]:
res = 0
for i in range(1,50001,1):
res += 1/i
print(res)
- 8
In [43]:
res = 0
for i in range(1,98,2):
res += i/ (i+2)
print(res)
- 9
In [48]:
res = 0
for i in range(1,100000):
res += 4*((-1)**(i+1)/(2*i-1))
print(res)
- 10
In [50]:
for i in range(1,10000):
res = 0
for j in range(1,i):
if i % j == 0:
res += j
if i == res:
print(i)
- 11
In [52]:
a = 0
for i in range(1,8,2):
for j in range(2,8):
if i != j:
print(i,j)
a += 1
- 12
In [17]:
import math
a = float(input())
b = float(input())
c = float(input())
d = float(input())
e = float(input())
f = float(input())
g = float(input())
h = float(input())
i = float(input())
j = float(input())
def Mean():
mean = (a+b+c+d+e+f+g+h+i+j) / 10
print('The mean is %.2f'%mean)
def Deviation():
mean = (a+b+c+d+e+f+g+h+i+j) / 10
deviation = (((a-mean)**2+(b-mean)**2+(c-mean)**2+(d-mean)**2+(e-mean)**2+(f-mean)**2+(g-mean)**2+(h-mean)**2+(i-mean)**2+(j-mean)**2)) / 10.0
print('The standard deviation is %.5f'%deviation)
Mean()
Deviation()