最近玩python,为了更好的学习其中的流程控制语句,自己写了一个查询员工信息和更新信息的脚本,此脚本仅供练习使用。大拿可绕行!
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#!/usr/bin/env python
import
sys
import
os
os.system(
'clear'
)
mess
=
'''
\033[;35m ID :%s\033[0m
\033[;35m Name :%s\033[0m
\033[;35m Dep :%s\033[0m
'''
info
=
'''
\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m
\033[;32m++ Welcome to staffs infomation system ++\033[0m
\033[;32m+-----------------------------------------+\033[0m
\033[;32m+ +\033[0m
\033[;32m+ S: search staffs infomation +\033[0m
\033[;32m+ U: update staffs infomation +\033[0m
\033[;32m+ Q: quit +\033[0m
\033[;32m+ +\033[0m
\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m
'''
info2
=
'''
\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m
\033[;32m+ A: Add staffs infomation +\033[0m
\033[;32m+ D: Del staffs infomation +\033[0m
\033[;32m+ Q: quit +\033[0m
\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m
'''
while
True
:
username
=
raw_input
(
'\033[;32mPlease input your name: \033[0m'
).strip()
passwd
=
raw_input
(
'\033[;32mPlease input password: \033[0m'
).strip()
if
username !
=
'zhaohh'
or
passwd !
=
'111111'
:
print
(
'\033[;31mYour username not exist or your password wrong!\033[0m'
)
continue
else
:
print
'\033[;32mHello zhaohh,Welcome!\033[0m'
break
os.system(
'sleep 2'
)
os.system(
'clear'
)
print
info
while
True
:
user_input
=
raw_input
(
"\033[;32mPlease select (S,U,default Q): \033[0m"
).strip()
# search staffs infomation
if
user_input
=
=
"s"
or
user_input
=
=
"S"
:
staff_list
=
open
(
"file.txt"
,
"rw"
)
c
=
staff_list.readlines()
staff_list.close()
while
True
:
inputs
=
raw_input
(
'\033[;32mPlease input ID or name or dep: \033[0m'
).strip()
for
line
in
c:
field
=
line.split()
if
inputs
in
field:
print
mess
%
(field[
0
],field[
1
],field[
2
])
break
else
:
juge
=
raw_input
(
'\033[;31mCan not found information! Continue? yes/no. \033[0m'
).strip()
if
juge
=
=
'yes'
or
juge
=
=
'y'
:
continue
else
:
break
juge
=
raw_input
(
'\033[;32mContinue search ? yes/no. \033[0m'
)
if
juge
=
=
'yes'
or
juge
=
=
'y'
:
continue
elif
juge
=
=
'no'
or
juge
=
=
'n'
or
juge
=
=
'':
sys.exit()
break
break
# add staffs infomation
elif
user_input
=
=
"u"
or
user_input
=
=
"U"
:
print
info2
while
True
:
update_input
=
raw_input
(
'\033[;32mPlease select (A,D,default Q): \033[0m'
).strip()
if
update_input
=
=
"a"
or
update_input
=
=
"A"
:
info_input
=
raw_input
(
"\033[;32mPlease input user infomation: \033[0m"
)
staff_list
=
open
(
"file.txt"
,
"a"
)
staff_list.write(info_input
+
'\n'
)
staff_list.close()
juge
=
raw_input
(
'\033[;32mUpdate successful! Continue search ? yes/no. \033[0m'
)
if
juge
=
=
'yes'
or
juge
=
=
'y'
:
continue
elif
juge
=
=
'no'
or
juge
=
=
'n'
or
juge
=
=
'':
sys.exit()
# delete staffs infomation
elif
update_input
=
=
"d"
or
update_input
=
=
"D"
:
inputs
=
raw_input
(
'\033[;32mPlease input ID or name or dep: \033[0m'
)
f
=
open
(
"newfile.txt"
,
"a"
)
staff_list
=
open
(
"file.txt"
,
"rw"
)
for
line
in
staff_list.readlines():
for
field
in
line.split():
if
inputs
=
=
field:
print
'\033[;32mDelete successful!\033[0m'
break
else
:
f.write(line)
staff_list.close()
f.close()
os.remove(
'file.txt'
)
os.rename(
'newfile.txt'
,
'file.txt'
)
juge
=
raw_input
(
'\033[;32mContinue? yes/no. \033[0m'
)
if
juge
=
=
'yes'
or
juge
=
=
'y'
:
continue
elif
juge
=
=
'no'
or
juge
=
=
'n'
or
juge
=
=
'':
sys.exit()
elif
update_input
=
=
"q"
or
update_input
=
=
"Q"
or
update_input
=
=
"":
sys.exit()
else
:
print
"\033[;31m Input error,try agin!\033[0m"
continue
elif
user_input
=
=
"q"
or
user_input
=
=
"Q"
or
user_input
=
=
"":
sys.exit()
else
:
print
"\033[;31m Input error,try agin!\033[0m"
continue
|
本文转自 baiying 51CTO博客,原文链接:http://blog.51cto.com/baiying/1252145,如需转载请自行联系原作者