循环
- 循环是一种控制语句块重复执行的结构
- 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)
JWUy
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]:
<str_iterator at 0x10b8863c8>
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)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-19-425af1773ecf> in <module>
----> 1 for i in True:
2 print(i)TypeError: 'bool' object is not iterable
在Python里面一切皆对象
EP:
In [2]:
i = 1
sum_ = 0
while sum_ < 10000:
sum_ += i
i += 1
print(sum_)
10011
In [1]:
sum_ = 0
for i in range(1,10001):
sum_ += i
if sum_ > 10000:
break
print(sum_)
10011
In [22]:
sum = 0
i = 0
while i < 1001:
sum = sum + i
i += 1
print(sum)
500500
嵌套循环
- 一个循环可以嵌套另一个循环
- 每次循环外层时,内层循环都会被刷新重新完成循环
- 也就是说,大循环执行一次,小循环会全部执行一次
- 注意:
- 多层循环非常耗时
- 最多使用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
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()
>>1
>>2
>>-1
>>3
>>0
3
1
1.25
In [7]:
def f():
dorlla = 10000
for i in range(14):
dorlla = dorlla * 0.05 + dorlla
if i == 9:
print(dorlla)
print(dorlla)
f()
16288.946267774414
19799.31599439397
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()
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()
120 150 180 210 240 270 300 330 360 390
420 450 480 510 540 570 600 630 660 690
720 750 780 810 840 870 900 930 960 990
In [15]:
n = 0
while n**2 < 12000:
n += 1
print(n)
110
In [17]:
n = 0
while n**3 < 12000:
n += 1
print(n-1)
22
In [18]:
n = 0
while 1:
res = n ** 2
if res >= 12000:
break
else:
n += 1
print(n)
110
In [21]:
n = 0
while 1:
res = n ** 3
if res < 12000:
n += 1
else:
break
print(n-1)
22
In [ ]:
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
5 % 月利率 500.0 总和 30000.0
5.125 % 月利率 512.5 总和 30750.0
5.25 % 月利率 525.0 总和 31500.0
.....
5.375 % 月利率 537.5 总和 32250.0
5.5 % 月利率 550.0 总和 33000.0
5.625 % 月利率 562.5 总和 33750.0
5.75 % 月利率 575.0 总和 34500.0
5.875 % 月利率 587.5 总和 35250.0
6.0 % 月利率 600.0 总和 36000.0
6.125 % 月利率 612.5 总和 36750.0
6.25 % 月利率 625.0 总和 37500.0
6.375 % 月利率 637.5 总和 38250.0
6.5 % 月利率 650.0 总和 39000.0
6.625 % 月利率 662.5 总和 39750.0
6.75 % 月利率 675.0 总和 40500.0
6.875 % 月利率 687.5 总和 41250.0
7.0 % 月利率 700.0 总和 42000.0
7.125 % 月利率 712.5 总和 42750.0
7.25 % 月利率 725.0 总和 43500.0
7.375 % 月利率 737.5 总和 44250.0
7.5 % 月利率 750.0 总和 45000.0
7.625 % 月利率 762.5 总和 45750.0
7.75 % 月利率 775.0 总和 46500.0
7.875 % 月利率 787.5 总和 47250.0
In [1]:
res = 0
for i in range(50000,0,-1):
res += 1/i
print(res)
11.397003949278519
In [2]:
res = 0
for i in range(1,50001,1):
res += 1/i
print(res)
11.397003949278504
In [43]:
res = 0
for i in range(1,98,2):
res += i/ (i+2)
print(res)
45.124450303050196
In [48]:
res = 0
for i in range(1,100000):
res += 4*((-1)**(i+1)/(2*i-1))
print(res)
3.1416026536897204
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)
6
28
496
8128
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
1 2
1 3
1 4
1 5
1 6
1 7
3 2
3 4
3 5
3 6
3 7
5 2
5 3
5 4
5 6
5 7
7 2
7 3
7 4
7 5
7 6
21
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()
1
2
3
4
5
6
7
8
9
10
The mean is 5.50
The standard deviation is 8.25000