循环
- 循环是一种控制语句块重复执行的结构
- 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 [1]:
def yunxing(zhengshu,fushu,sum_,cishu,data):
while data !=0 :
data = eval(input('输入一个整数>>'))
if data > 0:
zhengshu += 1
if data < 0:
fushu += 1
sum_ += data
if data != 0:
cishu += 1
else :
break
print('正数个数:%d'%zhengshu)
print('负数个数:%d'%fushu)
print(sum_ / cishu)
zhengshu = 0
fushu = 0
- 2
In [15]:
def aa():
dollar=10000
for i in range(14):
dollar += dollar * 0.05
if i == 9:
print(dollar)
if i == 10:
dollar1=dollar
if i == 11:
dollar2=dollar
if i == 12:
dollar3=dollar
print(dollar+dollar1+dollar2+dollar3)
aa()
- 3
In [ ]:
def yunxing(zhengshu,fushu,sum_,cishu,data):
while data !=0 :
data = eval(input('输入一个整数>>'))
if data > 0:
zhengshu += 1
if data < 0:
fushu += 1
sum_ += data
if data != 0:
cishu += 1
else :
break
print('正数个数:%d'%zhengshu)
print('负数个数:%d'%fushu)
print(sum_ / cishu)
zhengshu = 0
fushu = 0
In [1]:
- 4
In [16]:
def zcs(a,b):
count = 0
for i in range(a,b):
if i %5==0 and i % 6 == 0:
print(i,end=" ")
count += 1
if count % 10 == 0:
print()
zcs(100,1001)
- 5
In [18]:
def kk():
n = 0
while 1:
res = n ** 2
if res >= 12000:
break
else:
n += 1
print(n)
kk()
In [21]:
def jj():
n = 0
while 1:
res = n ** 3
if res < 12000:
n += 1
else:
break
print(n-1)
jj()
- 6
In [41]:
def cc():
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
cc()
- 7
In [2]:
def suan():
res = 0
for i in range(1,50001,1):
res += 1/i
print(res)
suan()
- 8
In [43]:
def MM()
res = 0
for i in range(1,98,2):
res += i/ (i+2)
print(res)
MM()
- 9
In [48]:
def pi():
res = 0
for i in range(1,100000):
res += 4*((-1)**(i+1)/(2*i-1))
print(res)
pi()
- 10
In [17]:
def a():
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)
a()
- 11
In [18]:
def math_():
a = 0
count = 0
for i in range(1,8,2):
for j in range(2,8):
if i != j:
print(i,j)
a += 1
count +=1
print('The total number of all combinations is %d'%count)
math_()
- 12
In [19]:
def shang():
import math
print('enter ten numbers:')
ls=[]
sum1=0.0
devi=0.0
for i in range(10):
num=eval(input(''))
ls.append(num)
part1=0.0
part2=0.0
for i in range(10):
sum1=sum1+ls[i]
mean=sum1/10
for i in range(10):
part1=part1+(ls[i]-mean)**2
#part2=sum1**2
#print(part1,part2)
devi=math.sqrt(part1/9)
print('The mean is %.2f'%(mean))
print('The standerd deviation %f'%(devi))
shang()