<jupyter_start><jupyter_text>循环- 循环是一种控制语句块重复执行的结构- while  适用于广度遍历- for    开发中经常使用  while 循环- 当一个条件保持真的时候while循环重复执行语句- while 循环一定要有结束条件，否则很容易进入死循环- while 循环的语法是：    while loop-contunuation-conndition:        Statement<jupyter_code>i = 0
while i<10:
    print('hahaha')
    i += 1<jupyter_output><empty_output><jupyter_text>示例：sum = 0i = 1while i <10:    sum = sum + i    i = i + 1  错误示例：sum = 0i = 1while i <10:    sum = sum + ii = i + 1- 一旦进入死循环可按 Ctrl + c 停止  EP：![](../Photo/143.png)![](../Photo/144.png)  验证码- 随机产生四个字母的验证码，如果正确，输出验证码正确。如果错误，产生新的验证码，用户重新输入。- 验证码只能输入三次，如果三次都错，返回“别爬了，我们小网站没什么好爬的”- 密码登录，如果三次错误，账号被锁定<jupyter_code>import random
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)
count = 0
for i in range(1000):
    a = random.randint(0,1000) / 1000
    if 0<a<0.001<jupyter_output><empty_output><jupyter_text>尝试死循环  实例研究：猜数字- 你将要编写一个能够随机生成一个0到10之间的且包括两者的数字程序，这个程序- 提示用户连续地输入数字直到正确，且提示用户输入的数字是过高还是过低  使用哨兵值来控制循环- 哨兵值来表明输入的结束- ![](../Photo/54.png)  警告![](../Photo/55.png)  for 循环- Python的for 循环通过一个序列中的每个值来进行迭代- range(a,b,k)， a，b，k 必须为整数- a: start- b: end- k: step- 注意for 是循环一切可迭代对象，而不是只能使用range<jupyter_code>for i in range(100):
    print('Joker is a better man!')
a = 100
bb = 'JOker'
bb.__iter__()
c = [1,2,3]
c.__iter__
{'key':'value'}.__iter__
(1,3,43).__iter__
{1,2,43}.__iter__
for i in range(5):
    print(i)<jupyter_output><empty_output><jupyter_text>在Python里面一切皆对象  EP:- ![](../Photo/145.png)<jupyter_code>i = 1
sum_ = 0
while sum_ < 10000:
    sum_ += i
    i += 1
print(sum_)
sum_ = 0
for i in range(1,10001):
    sum_ += i
    if sum_ > 10000:
        break
print(sum_)
sum = 0
i = 0
while i < 1001:
    sum = sum + i
    i += 1
print(sum)<jupyter_output>500500<jupyter_text>嵌套循环- 一个循环可以嵌套另一个循环- 每次循环外层时，内层循环都会被刷新重新完成循环- 也就是说，大循环执行一次，小循环会全部执行一次- 注意：> - 多层循环非常耗时  - 最多使用3层循环  EP：- 使用多层循环完成9X9乘法表- 显示50以内所有的素数  关键字 break 和 continue- break 跳出循环，终止循环- continue 跳出此次循环，继续执行<jupyter_code>for i in range(1,10):
    for j in range(1,i+1):
        print(j,'X',i,'=',i*j,end=' ')
    print()<jupyter_output>1 X 1 = 1 
1 X 2 = 2 2 X 2 = 4 
1 X 3 = 3 2 X 3 = 6 3 X 3 = 9 
1 X 4 = 4 2 X 4 = 8 3 X 4 = 12 4 X 4 = 16 
1 X 5 = 5 2 X 5 = 10 3 X 5 = 15 4 X 5 = 20 5 X 5 = 25 
1 X 6 = 6 2 X 6 = 12 3 X 6 = 18 4 X 6 = 24 5 X 6 = 30 6 X 6 = 36 
1 X 7 = 7 2 X 7 = 14 3 X 7 = 21 4 X 7 = 28 5 X 7 = 35 6 X 7 = 42 7 X 7 = 49 
1 X 8 = 8 2 X 8 = 16 3 X 8 = 24 4 X 8 = 32 5 X 8 = 40 6 X 8 = 48 7 X 8 = 56 8 X 8 = 64 
1 X 9 = 9 2 X 9 = 18 3 X 9 = 27 4 X 9 = 36 5 X 9 = 45 6 X 9 = 54 7 X 9 = 63 8 X 9 = 72 9 X 9 = 81<jupyter_text>注意![](../Photo/56.png)![](../Photo/57.png)  Homework- 1 ![](../Photo/58.png)<jupyter_code># Mine
z = 0
f = 0
num = 1
h = 0
l = 0
while num != 0:
    num = int(input('Enter an integer, the inpute ends if it is 0:'))
    if num > 0:
        z += 1
    if num < 0:
        f += 1
    h += num
    if num != 0:
        l += 1
print(z)
print(f)
print(h/l) 
zhengshu = 0
fushu = 0
sum_ = 0
cishu = 0
data = 1
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)<jupyter_output>>>1
>>-1
>>1
>>0
2
1
0.3333333333333333<jupyter_text>- 2![](../Photo/59.png)<jupyter_code># Mine
money = 10000
for i in range(14):
    money = 0.05 * money + money
    if i == 10:
        print('十年之后的学费：%f'%money)
print('十年之后和大学四年之后的学费：%f'%money)
dorlla = 10000
for i in range(14):
    dorlla = dorlla * 0.05 + dorlla
    if i == 9:
        print(dorlla)
print(dorlla)<jupyter_output>16288.946267774414
19799.31599439397<jupyter_text>- 3![](../Photo/58.png) - 4![](../Photo/60.png)<jupyter_code># Mine
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()
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()<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># Mine
i = 0
while 1:
    i += 1
    if i ** 2 > 12000:
        print(i)
        break
# Mine
i = 0
while i ** 3 < 12000:
    i += 1
print(i-1)
# Mine
i = 0
while 1:
    if i ** 3 < 12000:
        i += 1
    else:
        break
print(i-1)
n = 0
while n**2 < 12000:
    n += 1
print(n)
n = 0
while n**3 < 12000:
    n += 1
print(n-1)
n = 0
while 1:
    res = n ** 2
    if res >= 12000:
        break
    else:
        n += 1
print(n)
n = 0
while 1:
    res = n ** 3
    if res < 12000:
        n += 1
    else:
        break
print(n-1)<jupyter_output>22<jupyter_text>- 6![](../Photo/62.png)<jupyter_code># Mine
amount = float(input('Loan Amount:'))
year = float(input('Number of Years:'))
l = 0.05
count = 0
while l < 0.08:
    d = amount * l / year
    k = amount + (d * 12)   
    l = l + 1 / 800 
    print('年利率：%f  月还款：%.2f   年还款：%.2f'%(l,d,k)) 
           
    
# Mine
amount = float(input('Loan Amount:'))
year = float(input('Number of Years:'))
l = 0.05
count = 0
for i in range(24):
    d = amount * l / year
    k = amount + (d * 12) 
    if l <= 0.08:
        print('年利率：%f   月还款：%.2f   年还款：%.2f'%(l,d,k)) 
        l = l + 1 / 800 
    else:
        break
# Mine
amount = float(input('Loan Amount:'))
year = float(input('Number of Years:'))
def money():
    l = 0.05
    count = 0
    while l < 0.08:
        d = amount * l / year
        k = amount + (d * 12)   
        l = l + 1 / 800 
        print('年利率：%f  月还款：%.2f   年还款：%.2f'%(l,d,k)) 
money()
# Mine
amount = float(input('Loan Amount:'))
year = float(input('Number of Years:'))
def mo():
    l = 0.05
    count = 0
    for i in range(24):
        d = amount * l / year
        k = amount + (d * 12) 
        if l <= 0.08:
            print('年利率：%f   月还款：%.2f   年还款：%.2f'%(l,d,k)) 
            l = l + 1 / 800 
        else:
            break
mo()
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<jupyter_output>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<jupyter_text>- 7![](../Photo/63.png)<jupyter_code># Mine
h = 0
for i in range(1,50000):
    h = h + (1/i)
print(h)
# Mine
h2 = 0
for i in range(50000,0,-1):
    h2 = h2 + (1/i)
print(h2)
res = 0
for i in range(50000,0,-1):
    res += 1/i
print(res)
res = 0
for i in range(1,50001,1):
    res += 1/i
print(res)<jupyter_output>11.397003949278504<jupyter_text>- 8![](../Photo/64.png)<jupyter_code># Mine
sum_ = 0
for i in range(1,98,2):
    sum_ = sum_ + (i/ (i+2))
print(sum_)
res = 0
for i in range(1,98,2):
    res +=  i/ (i+2)
print(res)<jupyter_output>45.124450303050196<jupyter_text>- 9![](../Photo/65.png)<jupyter_code># Mine
for i in range(10000,100000,10000):
    n = 0
    for j in range(1,100000):
        n = n + 4 * (-1) ** (j + 1) / ((2 * j) - 1)
    print(n)
# Mine
def pi():
    n = 0
    for j in range(1,100000):
        n = n + 4 * (-1) ** (j + 1) / ((2 * j) - 1)
    print(n)
for i in range(10000,100000,10000):
    pi()
res = 0
for i in range(1,100000):
    res += 4*((-1)**(i+1)/(2*i-1))
print(res)<jupyter_output>3.1416026536897204<jupyter_text>- 10 ![](../Photo/66.png)<jupyter_code># Mine
q = 0
p = 0
for i in range(1,10000):
    a = 0
    for j in range(1,i):
        if i % j == 0:
            a = a + j
    if a == i:
        print(a)
# Mine
q = 0
p = 0
def fi(y):
    a = 0
    for j in range(1,y):
        if y % j == 0:
            a = a + j
    if a == y:
        print(a)
for i in range(1,10000):
    fi(i)
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)<jupyter_output>6
28
496
8128<jupyter_text>- 11![](../Photo/67.png)<jupyter_code># Mine
M = 7
N = 2
mn = 5
s1 = 1
s2 = 1
s3 = 1
count = 0
for a in range(1,8,2):
    for b in range(2,8):
        if a != b:
            print(a,b)
            count += 1
print('The total number of all combintions is %d'%count)        

# Mine
M = 7
N = 2
mn = 5
s1 = 1
s2 = 1
s3 = 1
# 阶乘
for a in range(1,8,2):
    for b in range(2,8):
        if a != b:
            print(a,b)
for i in range(M):
    M -= 1
    s1 = s * (M + 1)
    for j in range(N):
        N -= 1
        s2 = s2 * (N + 1)
        for z in range(mn):
            mn -= 1
            s3 = s3 * (mn + 1)
print('The total number of all combintions is %d'%(s1 / s2 / s3))
# Mine
M = 7
N = 2
mn = 5
s1 = 1
s2 = 1
s3 = 1
count = 0
def fun(y):
    global count
    for b in range(2,8):
        if y != b:
            print(y,b)
            count += 1
for a in range(1,8,2):
    fun(a)
print('The total number of all combintions is %d'%count) 
a = 0
for i in range(1,8,2):
    for j in range(2,8):
        if i != j:
            print(i,j)
            a += 1<jupyter_output>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<jupyter_text>- 12![](../Photo/68.png)<jupyter_code># Mine
count = 0
m = 0
dev = 0
print('Enter ten numbers:')
for i in range(1,11):
    a = float(input())
    count += 1
    m = m + a
    mean = m / count
    dev = ((a - mean) ** 2 + dev) / count 
print('The mean is %.2f'% mean)
print('The standard deviation is %f'% dev)<jupyter_output>Enter ten numbers:
1
2
3
5.5
5.6
6
7
8
9
10
The mean is 5.71
The standard deviation is 2.014252
{'fraction_non_alphanumeric': 0.16355685131195336, 'fraction_numerical': 0.13527696793002916, 'mean_word_length': 2.127013065937405, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '27376', 'license': 'Apache-2.0', 'n_tokens_mistral': 5997, 'n_tokens_neox': 5180, 'n_words': 1407, 'path': '7.19.ipynb', 'repo_name': 'KAJIA0001/Python', 'size': '10290'}