1、通过raw_input()函数获取用户输入(可见)
1
2
3
4
5
6
7
|
[root@Python scripts] # cat input.py
#!/usr/bin/env python #-*- coding:utf8 -*- raw_input ( "请输入用户名:" )
[root@Python scripts] # python input.py
请输入用户名:alex [root@Python scripts] #
|
2、将用户输入的内容复制给某个变量
1
2
3
4
5
6
7
8
9
|
[root@Python scripts] # cat input.py
#!/usr/bin/env python #-*- coding:utf8 -*- name = raw_input ( "请输入用户名:" )
print name
[root@Python scripts] # python input.py
请输入用户名:alex alex [root@Python scripts] #
|
3、用户输入不可见(getpass模块)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@Python scripts] # cat input.py
#!/usr/bin/env python #-*- coding:utf8 -*- import getpass
pwd = getpass.getpass( ">>>" )
print "####################"
print pwd
[root@Python scripts] # python input.py
>>> #################### alex [root@Python scripts] # python input.py
>>> #################### python |
4、流程控制
[root@Python scripts]# cat if01.py
1
2
3
4
5
6
7
8
9
10
11
|
#!/usr/bin/env python #-*- coding:utf8 -*- name = raw_input ( "请输入用户名:" ) #--->输入alex,此时两边都是alex,说明是两边是值在
#进行对比,而不是内存中同一个对象
if name = = "alex" : #--->注意:进行赋值的时候用一个等号(=),
#如果是进行比较用双等号(==)
#这里是值和值进行对比,不是内存地址进行对比。
print "登录成功"
else :
print "登录失败"
[root@Python scripts] #
|
is和双等于号(==)的区别:
== 指的是两个变量的值相等就以了,不要求是内存里的同一块地址
is 指的是两个变量在内存里必须是同一块地址,而不是说两个值相等就可以了
[root@Python scripts]# cat if01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/usr/bin/env python #-*- coding:utf8 -*- name = raw_input ( "请输入用户名:" )
if name = = "alex" :
print "登录成功"
else :
print "登录失败"
################################################ name = raw_input ( "请输入用户名:" ) #输入alex
name = raw_input ( "请输入用户名:" ) #输入alex
if name = = "eric" :
print "普通"
elif name = = "tony " :
print "超级"
elif name = = "alex" :
print "超神"
else :
print "非法"
|
控制脚本示例①:
[root@Python scripts]# cat login01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/env python #-*- coding:utf8 -*- import getpass
name = raw_input ( "请输入用户名:" )
pwd = getpass.getpass( "请输入密码:" )
if name = = "eric" and pwd = = "123" :
print "登录成功!普通,123"
elif name = = "tony" and pwd = = "123" :
print "登录成功!超级,123"
elif name = = "alex" and pwd = = "123" :
print "登录成功!超神,123"
else :
print "登录失败!"
|
将上述脚本进行改写:
[root@Python scripts]# cat login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/usr/bin/env python #-*- coding:utf8 -*- import getpass
name = raw_input ( "请输入用户名:" )
pwd = getpass.getpass( "请输入密码:" )
if name = = "alex" or name = = "eric" or name = = "tony" and pwd = = "123" :
print "登录成功!"
if name = = "alex" :
print "超神,123"
if name = = "eric" :
print "普通,123"
if name = = "tony" :
print "超级,123"
else :
print "非法用户"
print "登录失败!"
|
将脚本进一步改写:
[root@Python scripts]# cat login02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/usr/bin/env python #-*- coding:utf8 -*- import getpass
name = raw_input ( "请输入用户名:" )
pwd = getpass.getpass( "请输入密码:" )
if pwd = = "123" :
if name = = "eric" :
print "eric 普通"
elif name = = "tony" :
print "tony 超级"
elif name = = "alex" :
print "alex 超神"
else :
print "非法用户"
else :
print "登录失败!"
|
本文转自027ryan 51CTO博客,原文链接:http://blog.51cto.com/ucode/1709386,如需转载请自行联系原作者