<jupyter_start><jupyter_text>循环- 循环是一种控制语句块重复执行的结构- while  适用于广度遍历- for    开发中经常使用  while 循环- 当一个条件保持真的时候while循环重复执行语句- while 循环一定要有结束条件，否则很容易进入死循环- while 循环的语法是：    while loop-contunuation-conndition:        Statement  示例：sum = 0i = 1while i <10:    sum = sum + i    i = i + 1<jupyter_code>i=0
s='abcd'
while i<4:
    print (s[i])
    i+=1<jupyter_output>a
b
c
d<jupyter_text>错误示例：sum = 0i = 1while i <10:    sum = sum + ii = i + 1- 一旦进入死循环可按 Ctrl + c 停止  EP：![](../Photo/143.png)![](../Photo/144.png)<jupyter_code>number = eval(input('s'))
max = number

while number != 0:
    number = eval(input('s'))
    if number > max:
        max = number
        
print(max)
print(number)
import random
a=str(random.randrange(0,9))+chr(random.randrange(97,122))+str(random.randrange(0,9))+chr(random.randrange(97,122))
c=str(a)
print(c)
i=0
yzm= str(input('please input yzm  '))
if yzm == c :
    print('logoin')
else :
    print('loginout')
    while i<4:
        b=str(random.randrange(0,9))+chr(random.randrange(97,122))+str(random.randrange(0,9))+chr(random.randrange(97,122))
        d=str(b)
        print(d)
        yzm= str(input('please input yzm  '))
        if yzm == b :
            print('logoin')
            break
        else:
            print('loginout')
            i+=1
import matplotlib.pyplot as plt
x_list = []
y_list = []

for x in range(-50,50,1):
    y = x**2
    x_list.append(x)
    y_list.append(y)
print(x_list)
print(y_list)
plt.plot(x_list,y_list)
plt.show()<jupyter_output>[-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[2500, 2401, 2304, 2209, 2116, 2025, 1936, 1849, 1764, 1681, 1600, 1521, 1444, 1369, 1296, 1225, 1156, 1089, 1024, 961, 900, 841, 784, 729, 676, 625, 576, 529, 484, 441, 400, 361, 324, 289, 256, 225, 196, 169, 144, 121, 100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]<jupyter_text>验证码- 随机产生四个字母的验证码，如果正确，输出验证码正确。如果错误，产生新的验证码，用户重新输入。- 验证码只能输入三次，如果三次都错，返回“别爬了，我们小网站没什么好爬的”- 密码登录，如果三次错误，账号被锁定  尝试死循环  实例研究：猜数字- 你将要编写一个能够随机生成一个0到10之间的且包括两者的数字程序，这个程序- 提示用户连续地输入数字直到正确，且提示用户输入的数字是过高还是过低<jupyter_code>import random
a = random.randint(0,10)
print(a)
num = eval(input('enter a num  '))
while num != a :
    if num > a:
        print('smaller')
        num = eval(input('enter a num  '))
    else:
        print('larger')
        num = eval(input('enter a num  '))
print('bingo')<jupyter_output>10
enter a num  5
larger
enter a num  6
larger
enter a num  11
smaller
enter a num  10
bingo<jupyter_text>使用哨兵值来控制循环- 哨兵值来表明输入的结束- ![](../Photo/54.png)  警告![](../Photo/55.png)  for 循环- Python的for 循环通过一个序列中的每个值来进行迭代- range(a,b,k)， a，b，k 必须为整数- a: start- b: end- k: step- 注意for 是循环一切可迭代对象，而不是只能使用range  在Python里面一切皆对象  EP:- ![](../Photo/145.png)<jupyter_code>num = 0
su = 0

while num == 0:
    num = eval(input('enter a num'))
    su += num
print(su)<jupyter_output>enter a num2
2<jupyter_text>嵌套循环- 一个循环可以嵌套另一个循环- 每次循环外层时，内层循环都会被刷新重新完成循环- 也就是说，大循环执行一次，小循环会全部执行一次- 注意：> - 多层循环非常耗时  - 最多使用3层循环  EP：- 使用多层循环完成9X9乘法表- 显示50以内所有的素数<jupyter_code>for i in range(1,10):
    for j in range(1,10):
        while j<=i:
            k=j*i
            print(j,'*',i,'=',k,end ='   ')
            break
    print(end ='\n')
            
for i in range(2,51):
    for j in range(2,i):
        if (i%j)==0:
            break
    else:
        print(i)<jupyter_output>2
3
5
7
11
13
17
19
23
29
31
37
41
43
47<jupyter_text>关键字 break 和 continue- break 跳出循环，终止循环- continue 跳出此次循环，继续执行  注意![](../Photo/56.png)![](../Photo/57.png)  Homework- 1 ![](../Photo/58.png)<jupyter_code>k=0  
i=0
j=0
while 1:
    num = eval(input('enter an integer'))
    if num == 0:
        break
        print('正数',i,'负数',j,'平均数',l)
    else:
        k += num
        if num > 0:
            i += 1   
        
        elif num <0:
            j += 1
        
l = k/(i+j)
print(k,i,j,l)
print('正数',i,'负数',j,'平均数',l)<jupyter_output>enter an integer-1
enter an integer-5
enter an integer-2
enter an integer0
-8 0 3 -2.6666666666666665
正数 0 负数 3 平均数 -2.6666666666666665<jupyter_text>- 2![](../Photo/59.png)<jupyter_code>sum_=10000
for i in range(10):
    sum_=sum_*(1+0.05)
print(sum_)<jupyter_output>16288.946267774418<jupyter_text>- 3![](../Photo/58.png) - 4![](../Photo/60.png)<jupyter_code>a=0
for i in range(100,1000):
    if i%5==0 and i%6==0:
        a+=1
        print(i,end=' ')
        if a%10 == 0:
            print(end='\n')<jupyter_output>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<jupyter_text>- 5![](../Photo/61.png)<jupyter_code>import math
i=50
j=30
while i<200:
    if pow(i,2) >12000:
        break
    else:
        i+=1
print (i)
while j>20:
    if pow(j,3) <12000:
        break
    else:
        j-=1
print(j)<jupyter_output>110
22<jupyter_text>- 6![](../Photo/62.png)<jupyter_code>Amount = eval(input('Loan Amount:'))
Years = eval(input('Number of Years:'))
print("Interest Rate\t\tMonthly Payment\t\tTotal Payment")
n = 5
while n<8.1:
    Total = Amount*(1+n/100)**Years
    Month = (Total-10000)/12
    print(str(n)+"%","\t\t",Month,"\t\t",Total)
    n += 0.125<jupyter_output>Loan Amount:10000
Number of Years:5
Interest Rate		Monthly Payment		Total Payment
5% 		 230.23463541666692 		 12762.815625000003
5.125% 		 236.58048833262396 		 12838.965859991487
5.25% 		 242.9565955485838 		 12915.479146583006
5.375% 		 249.36306511546567 		 12992.356781385588
5.5% 		 255.8000053411456 		 13069.600064093747
5.625% 		 262.2675247907634 		 13147.21029748916
5.75% 		 268.76573228702847 		 13225.188787444342
5.875% 		 275.29473691052044 		 13303.536842926245
6.0% 		 281.8546480000003 		 13382.255776000004
6.125% 		 288.4455751527127 		 13461.346901832552
6.25% 		 295.06762822469074 		 13540.811538696289
6.375% 		 301.72091733106225 		 13620.651007972747
6.5% 		 308.4055528463538 		 13700.866634156246
6.625% 		 315.12164540479745 		 13781.45974485757
6.75% 		 321.8693059006343 		 13862.431670807611
6.875% 		 328.6486454884218 		 13943.783745861061
7.0% 		 335.4597755833336 		 14025.517307000004
7.125% 		 342.3028078614734 		 14107.633694337681
7.25% 		 349.1778542601725 	[...]<jupyter_text>- 7![](../Photo/63.png)<jupyter_code>n=0
for i in range(1,50000):
    n = n + 1/i
print(n)

m=0
j=50000
while j>=1:
    m= m + 1/j
    j-=1
print(m)<jupyter_output>11.396983949278505
11.397003949278519<jupyter_text>- 8![](../Photo/64.png)<jupyter_code>i=1
j=3
a=0
for i in range(1,98,2):
    for j in range(3,100,2):
        if i+2==j:
            a += i/j
print (a)<jupyter_output>45.124450303050196<jupyter_text>- 9![](../Photo/65.png)<jupyter_code>import math
pi = 0
for i in range(1,1000000000000):
    pi =pi+ 4*(pow((-1),(i+1))/(2*i-1))
    if i==100000000:
        print(pi)<jupyter_output><empty_output><jupyter_text>- 10 ![](../Photo/66.png)<jupyter_code>for i in range(1,10000):
    a=0
    for j in range(1,i):
        if i%j ==0:
            a += j
        
    if a==i:
        print (a)<jupyter_output>6
28
496
8128<jupyter_text>- 11![](../Photo/67.png)<jupyter_code>a=0
for i in range(1,8):
    for j in range(i,8):
        if i!=j:
            a+=1
            print (i,j)
print(a)<jupyter_output>1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
21<jupyter_text>- 12![](../Photo/68.png)<jupyter_code>1 **2 + 2**2 + 3**2 + 5.5 *2 + 5.6 **2 + 6 **2 + 7**2 + 8**2 + 9 **2 + 10 **2
1  + 2 + 3 + 5.5  + 5.6 + 6 + 7 + 8+ 9  + 10<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17217716781035558, 'fraction_numerical': 0.2230817217716781, 'mean_word_length': 2.9178885630498534, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '565323', 'license': 'Apache-2.0', 'n_tokens_mistral': 5205, 'n_tokens_neox': 4077, 'n_words': 837, 'path': '7.19.ipynb', 'repo_name': 'Yangxinyu524/53mx', 'size': '8015'}