为了以后更好更快速的复习,此博客记录我对作业的总结。对于基础作业,我认为最重要的是过程,至于实现是不是完美,代码是不是完美,虽然重要,但是作业过程中,用到的知识点是值得总结和整理的。
一.模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
思路:
1)输入时用户名后,先判断用户名是否被锁,然后检查用户名是否存在用户数据库;
2)输入相同不存在用户名3次,此用户名被锁;
3)输入用户名存在时才可输入密码,相同用户名时,输入密码错误三次,此用户名被锁;
4)若每次都没输入存在的用户,则再次要求输入用户,直到达到总输入次数;
基本流程图:
代码:
1
|
cat user_login.py #主程序
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#!#!/usr/bin/env python #_*_coding:utf-8_*_ ''' * Created on 2016/10/10 22:13.
* @author: Chinge_Yang.
''' import os
import getpass
retry_max = 3
retry_count = 0
user_file = "user.txt"
lock_file = "user_lock.txt"
user_same = 0
user_tmp = ""
if not os.path.exists(lock_file): #不存在时,则创建
f = open (lock_file, 'w' )
f.close()
while retry_count < retry_max:
#输入用户名
user_name = input ( "Please input your name:" )
#输入的用户名和上次输入的对比
if user_name = = user_tmp:
#用户一样数加1
user_same + = 1
else :
#用户一样数归0
user_same = 0
#输入的用户名存为临时变量
user_tmp = user_name
#判断用户是否被锁
lock_check = open (lock_file)
for line in lock_check:
line = line.split()
#用户被锁,打印提示
if user_name = = line[ 0 ]:
exit( "User \033[1;31;40m%s\033[0m is locked!" % user_name)
lock_check.close()
#查看是否存在于用户数据库
user_check = open (user_file)
for l in user_check:
l = l.split()
user = l[ 0 ]
passwd = l[ 1 ]
if user_name = = user:
#输入密码
#user_passwd = input("Please input your password:")
user_passwd = getpass.getpass( "\033[1;33;40mPlease input your password:\033[0m" )
#判断密码是否正确
if user_passwd = = passwd:
exit( "\033[1;32;40mWelcome to you!\033[0m" )
else :
print ( "User \033[1;31;40m%s\033[0m password is error!" % user_name)
#用户存在于数据库,跳出循环
break
else :
print ( "User \033[1;31;40m%s\033[0m not match in the user file!" % user_name)
user_check.close()
if user_same > = 2 :
print ( "User \033[1;31;40m%s\033[0m name was locked!" % user_name)
#将用户名写入到锁定文件中
file = open (lock_file, "a" )
file .write(user_name + "\n" )
file .close()
retry_count + = 1
|
user.txt文件内容格式:
1
|
cat user.txt
|
1
2
3
4
|
apache 123 jim 234 docker 333 fank yhn |
总结:
1.getpass模板用于输入密码时,不显示明文;
2.os.path.exists(文件路径)用于判断文件是否存在;
3.input的用法,版本3,默认输入的内容是字符类型,如果是变量名,则为此变量名的数据类型;
4.if判断语句用法;
5.while循环语句用法;
6.split()指定分隔符对字符串切片用法;
7.print(“%s” % name)等格式化输出;
8.颜色输出用法:手打出来 \033[32;1m内容\033[0m ;
9.数学算法使用;
二.三级菜单:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 菜单数据保存在文件中
思路:
1.菜单数据使用字典保存在文件中,使用import读取;
2.获取一级菜单;
3.获取二级菜单;
4.获取三级菜单;
5.使用循环打印菜单;
基本流程图:
代码:
1
|
cat Three_layer_menu.py #主程序
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/usr/bin/env python #_*_coding:utf-8_*_ ''' * Created on 2016/10/16 21:31.
* @author: Chinge_Yang.
''' import sys
import menu
while True :
#定义一层菜单为字典
one_dict = {}
#获取字典所有的键
one_layer = menu.menu.keys()
#获取字典所有的枚举
one_enu = enumerate (one_layer)
#一层菜单字典
for index1,value1 in one_enu:
one_dict[index1] = value1
for k1 in one_dict:
print ( "\033[32m%d\033[0m --> %s" % (k1,one_dict[k1]))
print ( "输入【back】:返回;【quit】:退出" )
once_select = input ( "请输入你的选择:\n" ).strip()
if once_select.isdigit():
once_select = int (once_select)
if 0 < = once_select < len (one_dict):
print ( "---->进入一级菜单 \033[32m%s\033[0m" % (one_dict[once_select]))
while True :
#定义二层菜单为字典
two_dict = {}
# 获取字典所有的键
two_layer = menu.menu[one_dict[once_select]].keys()
# 获取字典所有的枚举
two_enu = enumerate (two_layer)
# 二层菜单字典
for index2, value2 in two_enu:
two_dict[index2] = value2
# 打印二层菜单
for k2 in two_dict:
print ( "\033[32m%d\033[0m --> %s" % (k2, two_dict[k2]))
print ( "输入【back】:返回;【quit】:退出" )
twice_select = input ( "请输入你的选择:\n" ).strip()
if twice_select.isdigit():
twice_select = int (twice_select)
if 0 < = twice_select < len (two_dict):
print ( "---->进入二级菜单 \033[32m%s\033[0m" % (two_dict[twice_select]))
# 定义三层菜单列表
three_layer = menu.menu[one_dict[once_select]][two_dict[twice_select]]
while True :
# 打印三层菜单
for k3 in three_layer:
print (k3)
print ( "输入【back】:返回;【quit】:退出" )
three_select = input ( "请输入你的选择:\n" ).strip()
if three_select = = "back" :
break
elif three_select = = "quit" :
sys.exit ( "----谢谢使用----" )
continue
else :
if twice_select = = "back" :
break
elif twice_select = = "quit" :
sys.exit ( "----谢谢使用----" )
print ( "\033[31m请输入数字\033[0m" )
else :
print ( "----\033[31m数字超出范围,请重新输入!\033[0m----" )
else :
if once_select = = "back" :
break
elif once_select = = "quit" :
sys.exit ( "----谢谢使用----" )
print ( "\033[31m请输入数字\033[0m" )
|
1
|
cat menu.py
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#!/usr/bin/env python # _*_coding:utf-8_*_ ''' * Created on 2016/10/16 19:37. * @author: Chinge_Yang. ''' menu = {
"家用电器" : {
"电视" : [
"合资品牌电视" ,
"国产品牌电视" ,
"互联网品牌电视" ],
"空调" : [
"壁挂式空调" ,
"柜式空调" ,
"*空调" ,
"空调配件" ],
"洗衣机" : [
"滚筒洗衣机" ,
"洗烘一体机" ,
"波轮洗衣机" ,
"迷你洗衣机" ,
"洗衣机配件" ]
},
"电脑" : {
"电脑整机" : [
"笔记本" ,
"游戏本" "平板电脑" ,
"平板电脑配件" ,
"台式机" ,
"一体机服务器" ,
"笔记本配件" ],
"电脑配件" : [
"显示器" ,
"CPU" ,
"主板" ,
"硬盘" ,
"内存" ],
"外设产品" : [
"鼠标" ,
"键盘" ,
"键鼠套装" ,
"网络仪表仪器" ,
"U盘" ,
"移动硬盘" ]
}
}
|
总结:
1.字典的用法,包括获取key,value及相关函数用法;
2.列表的用法,遍历列表的用法;
3.import导入py文件用法;
本文转自 ygqygq2 51CTO博客,原文链接:http://blog.51cto.com/ygqygq2/1864705,如需转载请自行联系原作者