<jupyter_start><jupyter_text>循环- 循环是一种控制语句块重复执行的结构- while  适用于广度遍历- for    开发中经常使用  while 循环- 当一个条件保持真的时候while循环重复执行语句- while 循环一定要有结束条件，否则很容易进入死循环- while 循环的语法是：    while loop-contunuation-conndition:        Statement<jupyter_code>a = 'abcdef'
i = 0
while i<6:
    print(a[i],end=' ')
    i+=1<jupyter_output>a b c d e f<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 matplotlib.pyplot as plt
import random

import matplotlib.pyplot as plt
import random
i = 0
x=[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,7,8,9,1]
while i<20:
    x[i]=random.random()*10//1
    i+=1
    t = random.randint(0,1)
    if t == 0:
        x[i] = x[i]//(-1)
   # print(x[i])
        
y =[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,7,8,9,1]
i=0
while i<20:
    y[i]=x[i]**2
    i+=1
    
#list_ = [1,2,3,4,5,6,7]
#list__ = [3,4,5,6,7,8,9]
plt.plot(x,y)
plt.show()
import random
str1 = [random.randint(1,26)+64,random.randint(1,26)+64,random.randint(1,26)+64,random.randint(1,26)+64]
i=0
t=0
while i<4:
    t = random.randint(0,1)
    if t==0:
        str1[i] = chr(str1[i])
    elif t==1:
        str1[i] = chr(str1[i]+32)
    i+=1
    
i=0 
while i<4:
    print(str1[i],end='')
    i+=1


str2 = input('请输入验证码')<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  在Python里面一切皆对象  EP:- ![](../Photo/145.png)  嵌套循环- 一个循环可以嵌套另一个循环- 每次循环外层时，内层循环都会被刷新重新完成循环- 也就是说，大循环执行一次，小循环会全部执行一次- 注意：> - 多层循环非常耗时  - 最多使用3层循环  EP：- 使用多层循环完成9X9乘法表- 显示50以内所有的素数<jupyter_code>for i in range(1,10):
    for j in range(1,i+1):
        print(j,'*',i,'=',i*j,end=' \t',sep='')
    print()        
sum1 = 0
for i in range(1,51):
    for j in range(1,i+1):
        if i%j == 0:
            sum1+=1
        if sum1 == 2:
            break
    if j == i:
        print(i,'是素数')
    sum1=0<jupyter_output>1 是素数
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>number = eval(input('Enter an integer, end by 0: '))
sum1 = 0
sum1_1=0
sum2 = 0
while number != 0:
    if number>0:
        sum1 += 1
        sum1_1 += number
    elif number < 0:
        sum2 += 1
        sum1_1 += number
    number = eval(input('Enter an integer, end by 0: '))
print('正数有：',sum1,'个')
print('负数有：',sum2,'个')
print('平均值为',sum1_1/(sum1+sum2))<jupyter_output>Enter an integer, end by 0: 1
Enter an integer, end by 0: 2
Enter an integer, end by 0: 3
Enter an integer, end by 0: 4
Enter an integer, end by 0: 5
Enter an integer, end by 0: 6
Enter an integer, end by 0: 7
Enter an integer, end by 0: 8
Enter an integer, end by 0: 9
Enter an integer, end by 0: 10
Enter an integer, end by 0: -5
Enter an integer, end by 0: -2
Enter an integer, end by 0: 0
正数有： 10 个
负数有： 2 个
平均值为 4.0<jupyter_text>- 2![](../Photo/59.png)<jupyter_code>sum1 = 10000
sum2 = 10000
sum3 = 0
for i in range(1,11):
    sum1 += sum1*0.05
for i in range(1,14):
    sum2 += sum2*0.05
    if i>=10:
        sum3 += sum2
print(sum1)
print(sum2)
print(sum3)<jupyter_output>16288.946267774414
18856.491423232354
70207.39453239119<jupyter_text>- 3![](../Photo/58.png)<jupyter_code>number = eval(input('Enter an integer, end by 0: '))
sum1 = 0
sum1_1=0
sum2 = 0
while number != 0:
    if number>0:
        sum1 += 1
        sum1_1 += number
    elif number < 0:
        sum2 += 1
        sum1_1 += number
    number = eval(input('Enter an integer, end by 0: '))
print('正数有：',sum1,'个')
print('负数有：',sum2,'个')
print('平均值为',sum1_1/(sum1+sum2))<jupyter_output><empty_output><jupyter_text>- 4![](../Photo/60.png)<jupyter_code>i=30
j=1
while i*j<=1000:
    print(i*j,end='\t')
    if j%10 == 0:
        print()
    j+=1<jupyter_output>30	60	90	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>n = 100
while n**2<12000:
    n+=1
print(n-1)
n = 20
while n**3<12000:
    n+=1
print(n-1)<jupyter_output>109
22<jupyter_text>- 6![](../Photo/62.png)<jupyter_code>10000*1.05
percent1 = 0.05
while percent1 < = 0.08:<jupyter_output><empty_output><jupyter_text>- 7![](../Photo/63.png)<jupyter_code>i=1
sum1=0.0
while i<=50000:
    sum1+=1/i
    i+=1
print(sum1)


j=50000
sum1=0.0
while j>0:
    sum1= 1/j + sum1
    j-=1
print(sum1)<jupyter_output>11.397003949278504
11.397003949278519<jupyter_text>- 8![](../Photo/64.png)<jupyter_code>i = 1
j = 3
sum1=0
while j<=99:
    sum1+=i/j
    i=j
    j+=2
print('和为：',sum1)<jupyter_output>和为： 45.124450303050196<jupyter_text>- 9![](../Photo/65.png)<jupyter_code>i = 10000
pai = 0.0
while i>0:
    pai = ((-1) ** (i+1))/(2*i-1) + pai
    i -=1
    
print(pai*4)
    
i = 20000
pai = 0.0
while i>0:
    pai = ((-1) ** (i+1))/(2*i-1) + pai
    i -=1
print(pai*4)

i = 100000
pai = 0.0
while i>0:
    pai = ((-1) ** (i+1))/(2*i-1) + pai
    i -=1
print(pai*4)<jupyter_output><empty_output><jupyter_text>- 10 ![](../Photo/66.png)<jupyter_code>number = 6
while number < 10000:
    sum1=0
    for i in range(1,number):
        if number % i == 0:
            sum1+=i 
    if sum1 == number:
        print(number,'是完全数')
    number += 1<jupyter_output>6 是完全数
28 是完全数
496 是完全数
8128 是完全数<jupyter_text>- 11![](../Photo/67.png)<jupyter_code>sum1 = 0
for i in range(1,8):
    for j in range (1,8):
        if i != j:
            print(i,j)
            sum1 += 1
print(sum1)<jupyter_output>1 2
1 3
1 4
1 5
1 6
1 7
2 1
2 3
2 4
2 5
2 6
2 7
3 1
3 2
3 4
3 5
3 6
3 7
4 1
4 2
4 3
4 5
4 6
4 7
5 1
5 2
5 3
5 4
5 6
5 7
6 1
6 2
6 3
6 4
6 5
6 7
7 1
7 2
7 3
7 4
7 5
7 6
-7<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.17749054224464061, 'fraction_numerical': 0.13209331651954603, 'mean_word_length': 3.0935483870967744, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '435337', 'license': 'Apache-2.0', 'n_tokens_mistral': 3808, 'n_tokens_neox': 3507, 'n_words': 718, 'path': '7.19.ipynb', 'repo_name': 'qqdazhutizi/jizhirui', 'size': '6344'}