10 月 102020
 
password_list = ['*#*#', '12345']
def account_login():
    tries = 3 #赋值变量,初始密码重试次数为3
    while tries > 0: #while循环
        password = input('Password:') #用户输入赋值变量
        #密码为12345,则password_correct = True
        password_correct = password == password_list[-1] 
        #密码为*#*#,则password_reset = True
        password_reset = password == password_list[0]

        if password_correct: #密码正确,打印成功提示,继续while循环
            print('Login success!')
        elif password_reset: #重设密码正确,提示输入新密码
            new_password = input('Enter a new password:')
            password_list.append(new_password) #新密码写入列表
            print('Password has changed successfully!')
            account_login() #重新调用函数登录
        else: #反之,提示错误
            print('Wrong password or invalid input!')
            tries = tries - 1 #同时对重试次数减1,并重新赋值给tries变量
            print(tries, 'times left') #打印提示剩余可重试次数
    else: #当while循环为False时,即tries不大于0时,打印提示账户锁定
        print('Your account has been suspended')
        exit() #break只可以用在while和for循环中,在超过重试次数后退出

#调用函数
account_login()
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Password:12345
Login success!
Password:*#*#
Enter a new password:54321
Password has changed successfully!
Password:123
Wrong password or invalid input!
2 times left
Password:123
Wrong password or invalid input!
1 times left
Password:123
Wrong password or invalid input!
0 times left
Your account has been suspended
PS C:\Users\harveymei>
10 月 102020
 
#Python中的两种循环:for循环和while循环
#for循环在可迭代的序列被穷尽时停止
#while循环在条件不成立时停止,即只要……条件成立,就一直做……
while 1 < 3:
    print('1 is smaller than 3')
#永远为True的循环,称之为死循环,Ctrl+C强制停止运行
#赋值变量,计数目的,初始值为0
count = 0
#while循环
while True:
    print('Repeat this line!')
    count = count + 1 #每打印一行,对count加1并重新赋值给count
    if count == 5: #判断count当前是否为5,是则终止while循环
        break #关键词
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Repeat this line!
Repeat this line!
Repeat this line!
Repeat this line!
Repeat this line!
PS C:\Users\harveymei>
10 月 102020
 
#嵌套循环
#依次将1-9赋值给变量i
for i in range(1,10):
    for j in range(1,10): #依次将1-9赋值给变量j
        #使用format()字符串格式化函数填充字符串
        print('{} X {} = {}'.format(i, j, i*j))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
1 X 5 = 5
1 X 6 = 6
1 X 7 = 7
1 X 8 = 8
1 X 9 = 9
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
4 X 1 = 4
4 X 2 = 8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
8 X 1 = 8
8 X 2 = 16
8 X 3 = 24
8 X 4 = 32
8 X 5 = 40
8 X 6 = 48
8 X 7 = 56
8 X 8 = 64
8 X 9 = 72
9 X 1 = 9
9 X 2 = 18
9 X 3 = 27
9 X 4 = 36
9 X 5 = 45
9 X 6 = 54
9 X 7 = 63
9 X 8 = 72
9 X 9 = 81
PS C:\Users\harveymei> 
10 月 102020
 
#for循环和if条件判断
#定义列表并赋值给变量
songlist = ['Holy Diver', 'Thunderstruck', 'Rebel Rebel']
#循环从列表中取值赋值给变量
for song in songlist:
    if song == 'Holy Diver': #条件判断,比较当前song的取值
        print(song, ' - Dio') #打印歌曲名称和歌手名称
    elif song == 'Thunderstruck':
        print(song, ' - AC/DC')
    elif song == 'Rebel Rebel':
        print(song, ' - David Bowie')
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Holy Diver  - Dio
Thunderstruck  - AC/DC
Rebel Rebel  - David Bowie
PS C:\Users\harveymei> 
10 月 102020
 
#for循环
for num in range(1,11):
    #range()函数,输出连续的整数序列,起始start,终止stop,不包含终止值
    #将range()函数的值,循环赋值给变量num
    #str()函数将整数转换为字符串,打印字符串,并拼接计算结果
    print(str(num) + '+ 1 =', num + 1)
	print(range(1,11))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
1+ 1 = 2
2+ 1 = 3
3+ 1 = 4
4+ 1 = 5
5+ 1 = 6
6+ 1 = 7
7+ 1 = 8
8+ 1 = 9
9+ 1 = 10
10+ 1 = 11
PS C:\Users\harveymei>