一、内容
二、练习
练习1
题目:使用while循环输出1 2 3 4 5 6 8 9 10
方法一:
图示:
代码:
count = 1
while count < 11:if count != 7:print(count)count += 1
输出结果:
1 2 3 4 5 6 8 9 10
方法二:
图示:
代码:
count = 0
while count < 10:count += 1if count == 7:continueprint(count)
输出结果:
1 2 3 4 5 6 8 9 10
练习2
题目:求1-100的所有数的和
图示:
代码:
sum = 0
count = 1
while count < 101:sum = sum + countcount += 1
print(sum)
输出结果:
5050
练习3
题目:输出 1-100 内的所有奇数
图示:
代码:
count = 1
while count < 101:if count % 2 != 0:print(count)count += 1
输出结果:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
练习4
题目:输出 1-100 内的所有偶数
图示:
代码:
count = 1
while count < 101:if count % 2 == 0:print(count)count += 1
输出结果:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
练习5
题目:求1-2+3-4+5 ... 99的所有数的和
图示:
代码:
count = 1
sum1 = 0
sum2 = 0
while count < 100:if count % 2 != 0:sum1 = sum1 + countelse:sum2 = sum2 - countcount += 1
print(sum1 + sum2)
输出结果:
50
练习6
题目:用户登陆(三次机会重试)
步骤一:实现三次登陆的基本功能
图示:
代码:
user = 'knight'
pwd = 'dk123'
count = 1
while True:if count == 4:print('Too many times!')breakusername = input('Please enter your username:')password = input('Please enter your password:')if username == user and password == pwd:print('Login successfully!')breakelse:print('The username or password you entered is incorrect,Please try again!')count += 1
步骤二:去除BUG并添加功能
1、去除用户输入用户名时左右两边的空格
2、判断用户名是否有值,如果没有则反复让用户输入,并提示请输入用户名
3、判断密码是否有值,如果没有则反复让用户输入,提示请输入密码
4、用户每输错一次便告之用户还有多少次机会
图示:
代码:
user = 'knight'
pwd = 'dk123'
count = 1
while True:if count == 4:print('Too many times!')breakusername = input('Please enter your username:').strip()password = input('Please enter your password:')if not username:print('The username you entered is blank, please re-enter')continueelif not password:print('The password you entered is blank, please re-enter')continueif username == user and password == pwd:print('Login successfully!')breakelse:print('The username or password you entered is incorrect,Please try again!')print('You still have %s chances'%(3-count))count += 1
步骤三:三次登陆锁定
扩展:当同一个用户名的输错次数超过三次时,锁定该用户名
图示:
代码:
user = 'knight'
pwd = 'dk123'
count = 1
times = [] # 用于统计密码输错时的用户名的个数with open('blacklist.txt', 'r', encoding='utf-8') as f_black:black_line = f_black.readlines() # 将黑名单内容以列表的形式读出来# print(black_line)while True:# 当登陆次数超过3次时提示登陆次数过多,退出程序if count == 4:print('Too many times!')breakusername = input('Please enter your username:').strip()password = input('Please enter your password:').strip()# 判断用户名是否为空if not username:print('The username you entered is blank, please re-enter')continue# 判断密码是否为空elif not password:print('The password you entered is blank, please re-enter')continue# 判断用户名再加上"\n"是否在黑名单的列表里,如果在则提示用户已锁定再退出程序if username + '\n' in black_line:print('Sorry, your account has been locked!')break# 判断用户名和密码是否匹配if username == user and password == pwd:print('Login successfully!')breakelse:print('The username or password you entered is incorrect,Please try again!')print('You still have %s chances' % (3 - count)) # 如果不匹配则提示用户名或密码错误并告之用户还有几次机会times.append(username) # 将错误的用户名加入至事先定义的times列表中,if times.count(username) == 3: # 然后计算加入的用户名的个数是否等于三个,如果是则写入至黑名单文件中。with open('blacklist.txt', 'a', encoding='utf-8')as f:f.write(username+'\n')count += 1
练习7
题目:猜年龄
步骤一:实现基本逻辑
图示:
代码:
age = 28
while True:user = int(input('Please enter a number:')) # 强转为整型if user > age:print('Try smaller!')elif user < age:print('Try bigger!')else:print('You got it!')break
步骤二:去除BUG并添加功能
1、去除BUG,当用户输入其它字符时让其重新输入
2、添加猜错时给予三次机会功能
图示:
代码:
age = 28
count = 0
while True:if count == 3:print('You guess too many times')breakuser = input('Please enter a number:').strip()if not user.isdigit():print('Please try again,and you must enter a number!')continueelse:user = int(user)if user > age:print('Try smaller!')elif user < age:print('Try bigger!')else:print('You got it!')breakcount += 1
三、英语单词
必 须 背 诵 出 来 !
1、Programming
['proɡræmɪŋ] n. 编程
2、Programming language
['proɡræmɪŋ] ['læŋɡwɪdʒ] n.编程语言
3、high-level language
[,haɪ'lɛvl] ['læŋɡwɪdʒ] n.高级语言
4、machine language
[mə'ʃin] ['læŋɡwɪdʒ] n.机器语言
5、assembly language
[ə'sɛmbli] ['læŋɡwɪdʒ] n.汇编语言
6、variable
['vɛrɪəbl] n. 变量
7、memory
['mɛməri] n.内存
8、binary
['baɪnəri] n.二进制
9、decimalism
['desiməlizəm] n.十进制
10、octal
['ɑktl] adj.八进制的
11、hexadecimal
[,hɛksə'dɛsɪml] n.十六进制
12、file
[faɪl] n.文件
13、directory
[dəˈrɛktəri; (also) daɪˈrɛktəri] n.目录
14、input
['ɪn'pʊt] vt.输入