一、while循环

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

1、基本循环

while循环语句基本结构:

[初始化部分一般是用来定义循环变量]
while 循环条件:
    循环体语句
    [循环变量更改部分]
[els:
    语句体]


执行顺序:
1. 初始化部分,一般是用了定义循环变量或新赋值
2.判断循环条件:
1 2 3 4 5 6 while  条件:            # 循环体        # 如果条件为真,那么循环体则执行      # 如果条件为假,那么循环体不执行

 

2、无限循环

# 死循环
while True:
    print('海草')
    print('女儿情')
    print('二泉映月')
    print('牧马城市')

 

*如何终止循环*

1,改变条件。
2,break
3,调用系统命令:quit() exit() (不建议使用)

3、break

break:循环中遇到break 直接退出循环.
1 count = 0
2 while count < 10:
3     count = count +1
4     print (count)
5     break
6     print(123)
7 print("end")
 
 

3、continue

continue用于退出当前循环,继续下一次循环

1 count = 0
2 while count < 10:
3     count = count +1
4     print (count)
5     continue
6     print(123)
7     print ("end")

 

总结:

break:停止:直接停止当前的循环,不论还剩下多少次循环。
continue:跳过当前循环后面的语句,直接执行下一轮循环。

注意事项:

 

1, 对于计数器,如果是从0开始,条件判断中,一般不会出现等号

2,如果从哦那开始,一般条件会出现等号

3, sum 是一个内置函数名,最好不要和内置函数名重名

4,求和思想

# 先定义一个变量,用于保存所有数的和,初始值一定为0
# 每次累加变量
# 定义求和变量,初始值一定是0

5,求积思想


# 定义一个变量用于保存所有数的乘积,初始值一定为1
# 每一次都乘以变量的值再把结果赋值

 

 

练习题

一,输出(1~100)的整数:

1 count = 1
2 while count < 101:
3     print(count)
4     count = count + 1

 

二,用while循环输出 1 2 3 4 5 6 7 8 9 10:

1 count = 0
2 while count < 10 :
3     count = count +1
4     print(count,' ',end='')

 

三,用while循环输出 1 2 3 4 5 6   8 9 10:

Python( Day2 ) :while循环 Python 第1张
 1 #第一种
 2 count = 0
 3 while count < 10:
 4     if count == 7:
 5         count = count + 1
 6         continue
 7     print (count,' ',end='')
 8     count = count + 1
 9 
10 
11 
12 #第二种
13 n = 1
14 while n < 10:
15     n = n + 1
16     if n == 7:
17         continue
18     else:
19     print(n)
20 
21 
22 
23 
24 #第三种
25 n = 1
26 while n < 10:
27     n = n + 1
28     if n == 7:
29         pass
30     else:
31         print(n)
32 
33 
34 
35 #第四种
36 count = 0
37 while count < 10:
38     count +=1         # count = count + 1
39     if count == 7:
40         print('')
41     else:
42         print(count)
View Code

 

 

四,打印(1~100)的偶数:

 1 #第一种
 2 count = 1
 3 while count < 101:
 4     if count % 2 == 0:
 5         print(count)
 6     else:
 7         pass
 8     count = count+1
 9 print('END')
10 
11 #第二种
12 count = 1
13 while count < 101:
14     if count % 2 == 0:
15         print(count)
16     count += 1
17 
18 #第三种
19 count = 1
20 while count < 101:
21     print(count)
22     count += 1
23 '''

 

五,打印(1~100)的奇数:

#第一种
count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
    else:
        pass
    count = count+1
print('END')

#第二种 count = 1 while count < 101: if count % 2 == 1: print(count) count += 1
#第三种 count = 1 while count < 101: print(count) count +=2

 

六,求1~100所有整数的和:

1 count1 = 0
2 count2 = 1
3 while count2 < 101:
4     count1 = count1 + count2
5     count2 = count2 + 1
6 print (count1)

 

七,求1-2+3-4+5 ...... 99的所有数的和:

 1 # 1+3+5+7+9....+99 - 2-4-6-8-10
 2 
 3 sum = 0
 4 count = 1
 5 while count <= 100:
 6     if count % 2 == 1:     # 奇数
 7         sum += count       # sum = sum + count         
 8     else:                  # 偶数
 9         sum -= count       # sum = sum - count
10     count += 1             # count = count + 1
11 print(sum)

 

八,利用while语句写出猜⼤⼩的游戏:设定⼀个理想数字⽐如:66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测的结果⼤了;

    如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果正确,然后退出循环

 1 count = 1
 2 n = 66
 3 while True:
 4     num = int(input())
 5     if num > n:
 6         print('猜大了')
 7     elif num < n:
 8         print('猜小了')
 9     else:
10         print('猜对了')
11         break
12     count = count + 1

 

 

九,在上题基础上升级:给⽤户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。

 1 count = 1
 2 n = 66
 3 while count < 3:
 4     num = int(input())
 5     if num > n:
 6         print('猜大了')
 7     elif num < n:
 8         print('猜小了')
 9     else:
10         print('猜对了')
11         break
12     count = count + 1
13 else:
14     print('太笨了')

 

 

十,在上题的基础上再次升级:变成随机猜数字

 

 1 import random                 # from random import randint
 2 n = random.randint(1,100)     # n = randint(1,100)
 3 count = 1
 4 while True:
 5     num = int(input('请猜:'))
 6     if num > n:
 7         print('猜大了')
 8     elif num < n:
 9         print('猜小了')
10     else:
11         print('猜对了')
12         break
13     count = count + 1

 

十一,⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

 

 1 count = 1
 2 while count <= 3:
 3     username = input('请输入姓名:')
 4     password = int(input('请输入密码:'))
 5     if username == 'wyf' and password == 123:
 6         print('恭喜你登录成功!')
 7         break
 8     else:
 9         print('登陆失败')
10     print('当前登陆了%s次,剩余登录次数为%s次' % (count, 3 - count))
11     count = count + 1

 

方法二:

 

Python( Day2 ) :while循环 Python 第3张
 1 username = "wyf"
 2 password = "123"
 3 n = 3
 4 while n>0:
 5     name = input("请输入姓名:")
 6     n = n -1
 7 
 8     if name == username:
 9         psw = input('请输入密码:')
10         if psw == password:
11             print('登陆成功,请稍后...')
12             print('''
13              username: %s
14              password: %s
15             ''' % (username,password)
16             )
17             break
18         else:
19             print('你的密码输入错误,请重新输入:')
20             print('你还有%s次机会' % (n))
21             if n== 0 :
22                 print('你的机会已经用完')
23                 # answer = input('在试试?  Yes or No')
24                 # if answer == 'Yes':
25                 #     i = 3
26                 break
27 
28 
29     else:
30         print("你的用户名错误,请重新输入!")
31         # print('你还有%s次机会' % (n))
32         print("你还有" + str(n) + "次机会")
33         if n ==0:
34             print('你的机会已经用完')
35             # answer = input ('在试试?  Yes or No')
36             # if answer == 'Yes':
37             #     i = 3
38             break
View Code

 

 

 练习题

 

Python( Day2 ) :while循环 Python 第5张
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
# Author : YiFan        Time : 2019-02-28 19:51:21

"""  优先级,()> not > and > or"""
""" and:有假为假       or:有真为真"""
'''x or y , x为真,值就是x,x为假,值是y;'''


'''
#1

print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)           # T
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)          # F
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)      # F


#2
print(8 or 3 and 4 or 2 and 0 or 9 and 7)      # 8

print(0 or 2 and 3 and 4 or 6 and 0 or 3)      # 4


#3
print(6 or 2 > 1)    # 6
print(3 or 2 > 1)    # 3
print(0 or 5 < 4)    # False
print(5 < 4 or 3)    # 3
print(2 > 1 or 6)    # True
print(3 and 2 > 1)   # True
print(0 and 3 > 1)   # 0
print(2 > 1 and 3)   # 3
print(3 > 1 and 0)   # 0
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2) #2
'''

'''
#4.while循环语句基本结构?

[初始化部分一般是用来定义循环变量]
while 循环条件:
    循环体语句
    [循环变量更改部分]
[els:
    语句体]
    
执行顺序:
1. 初始化部分,一般是用了定义循环变量或新赋值
2.判断循环条件:


'''


'''
# 5 利⽤while语句写出猜⼤⼩的游戏:
#   设定⼀个理想数字⽐如:66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测的结果⼤了;
#   如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果正确,然后退出循环

count = 1
n = 66
while True:
    num = int(input())
    if num > n:
        print('猜大了')
    elif num < n:
        print('猜小了')
    else:
        print('猜对了')
        break
    count = count + 1
'''



'''
#对于计数器,如果是从0开始,条件判断中,一般不会出现等号
#如果从哦那开始,一般条件会出现等号

# 6 在5题的基础上进⾏升级:
#   给⽤户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果
#   三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。


count = 1
n = 66
while count < 3:
    num = int(input())
    if num > n:
        print('猜大了')
    elif num < n:
        print('猜小了')
    else:
        print('猜对了')
        break
    count = count + 1
else:
    print('太笨了')


'''


'''
#在第六题的基础上升级,随机的猜出数字


import random                 # from random import randint
n = random.randint(1,100)     # n = randint(1,100)
count = 1
while True:
    num = int(input('请猜:'))
    if num > n:
        print('猜大了')
    elif num < n:
        print('猜小了')
    else:
        print('猜对了')
        break
    count = count + 1

'''


'''
# 7.使⽤while循环输出 1 2 3 4 5 6 8 9 10

count = 0
while count < 10 :
    count = count +1
    print(count,' ',end='')
'''


'''
#使用while循环输入 1 2 3 4 5 6   8 9 10

#第一种
count = 0
while count < 10:
    if count == 7:
        count = count + 1
        continue
    print (count,' ',end='')
    count = count + 1
'''

'''
#第二种
n = 1
while n < 10:
    n = n + 1
    if n == 7:
        continue
    else:
    print(n)




#第三种
n = 1
while n < 10:
    n = n + 1
    if n == 7:
        pass
    else:
        print(n)




#第四种
count = 0
while count < 10:
    count +=1         # count = count + 1
    if count == 7:
        print('')
    else:
        print(count)
'''




'''
# sum 是一个内置函数名,最好不要和内置函数名重名

#求和思想
#先定义一个变量,用于保存所有数的和,初始值一定为0
#每次累加变量
#定义求和变量,初始值一定是0

#乘积思想
#定义一个变量用于保存所有数的乘积,初始值一定为1
#每一次都乘以变量的值再把结果赋值


# 8.求1-100的所有整数的和

count1 = 0
count2 = 1
while count2 < 101:
    count1 = count1 + count2
    count2 = count2 + 1
print (count1)

'''

'''
#9.输出 1-100 内的所有奇数

#第一种
count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
    else:
        pass
    count = count+1
print('END')

#第二种
count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
    count += 1

#第三种
count = 1
while count < 101:
    print(count)
    count +=2

'''



'''
#10.输出 1-100 内的所有偶数
#第一种
count = 1
while count < 101:
    if count % 2 == 0:
        print(count)
    else:
        pass
    count = count+1
print('END')

#第二种
count = 1
while count < 101:
    if count % 2 == 0:
        print(count)
    count += 1

#第三种
count = 1
while count < 101:
    print(count)
    count += 1
'''


'''
#11.求1-2+3-4+5 ... 99的所有数的和
# 1+3+5+7+9....+99 - 2-4-6-8-10

sum = 0
count = 1
while count <= 100:
    if count % 2 == 1: # 奇数
        sum += count   # sum = sum + count         
    else:              # 偶数
        sum -= count   # sum = sum - count
    count += 1         # count = count + 1
print(sum)
'''



'''
#12.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

count = 1
while count <= 3:
    username = input('请输入姓名:')
    password = int(input('请输入密码:'))
    if username == 'wyf' and password == 123:
        print('恭喜你登录成功!')
        break
    else:
        print('登陆失败')
    print('当前登陆了%s次,剩余登录次数为%s次' % (count, 3 - count))
    count = count + 1
'''


"""
username = "wyf"
password = "123"
n = 3
while n>0:
    name = input("请输入姓名:")
    n = n -1

    if name == username:
        psw = input('请输入密码:')
        if psw == password:
            print('登陆成功,请稍后...')
            print('''
             username: %s
             password: %s
            ''' % (username,password)
            )
            break
        else:
            print('你的密码输入错误,请重新输入:')
            print('你还有%s次机会' % (n))
            if n== 0 :
                print('你的机会已经用完')
                # answer = input('在试试?  Yes or No')
                # if answer == 'Yes':
                #     i = 3
                break


    else:
        print("你的用户名错误,请重新输入!")
        # print('你还有%s次机会' % (n))
        print("你还有" + str(n) + "次机会")
        if n ==0:
            print('你的机会已经用完')
            # answer = input ('在试试?  Yes or No')
            # if answer == 'Yes':
            #     i = 3
            break
"""





'''
#13.简述ASCII、Unicode、utf-8编码
ASCII:   早期ASCII 用7位表示一个字符,但为了以后的发展,增加了一位,
         用8位表示一个字符,但是 8位只有256种可能,数量太少,创办了Unicode 万国码
Unicode: 刚开始用16位表示一个字符,升级成用32位表示一个字符,但是32位的太浪费空间了,然后从Unicode升级到了utf-8
utf-8:   一个字符最少用8位进行表示,英文用8位,一个字节;欧洲文字用16位,两个字节;中文用24位,三个字节

'''


'''
#14.简述位和字节的关系?
# 1bit    8bit = 1bytes
# 1byte   1024byte = 1KB
# 1KB     1024kb = 1MB
# 1MB     1024MB = 1GB
# 1GB     1024GB = 1TB

'''


'''
#明⽇默写内容:
#1. 求1~100所有数的和。

s = 0
n = 1
while n < 101:
    s = s + n
    n = n + 1
print (s)


2. break continue的含义区别
break    停止,直接停止当前的循环,不论剩下多少内容,用于退出所有循环
continue 用于退出当前循环,继续下一次循环


3,Unicode,utf-8,GBK,每个编码英⽂,中⽂,分别⽤⼏个字节表示。
           英文       中文
Unicode   两个字节    两个字节
utf-8     一个字节    三个字节
GBK       一个字节    两个字节
'''
while 练习题

 

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄