您好,登錄后才能下訂單哦!
1、單條件流程控制
If:
else:
2、多條件的流程控制
If xxx:
print yyy4
elif xxxx:
print yyy3
elif:
print yyy2
else:
print yyy1
eg:修改ex1.py
#!/usr/bin/env python
# _*_ coding = utf-8 _*_
import getpass
name = raw_input('please inputyour username:')
pwd = getpass.getpass('inputyour password:')
print 'the name is:',name
print 'the password is:',pwd
if name == 'huwei' and pwd =='huwei123':
print 'you login successfully!,welcomehuwei,normal user'
elif name == 'tony' and pwd =='123':
print 'you login successfully!,welcometony,super user'
elif name == 'humin' and pwd =='123':
print 'you login successfully!,welcomehumin,superhero user'
else:
print 'your password is wrong!'
執行結果:
[root@localhost~]# python ex1.py
please input yourusername:huwei
input yourpassword:
the name is:huwei
the password is:huwei123
you loginsuccessfully!,welcome huwei,normal user
[root@localhost~]# python ex1.py
please input yourusername:humin
input yourpassword:
the name is:humin
the password is:123
you loginsuccessfully!,welcome humin,superhero user
[root@localhost~]# vi ex1.py
[root@localhost~]# python ex1.py
please input yourusername:tony
input yourpassword:
the name is: tony
the password is:123
you loginsuccessfully!,welcome tony,super user
[root@localhost~]#
eg:
變種ex1.py:
import getpass
name = raw_input('please inputyour username:')
pwd =getpass.getpass('password:')
if pwd == '123':
if name == 'tony':
print 'tony,normal'
elif name == 'huwei':
print 'huwei,super'
elif name == 'humin':
print 'humin,superhero'
else:
print 'password is wrong!'
else:
print 'password is wrong!'
執行結果:
[root@localhost~]# python ex1.py
please input yourusername:huwei
input yourpassword:
the name is:huwei
the password is:huwei123
you loginsuccessfully!,welcome huwei,normal user
[root@localhost~]# python ex1.py
please input yourusername:humin
input yourpassword:
the name is:humin
the password is:123
you loginsuccessfully!,welcome humin,superhero user
[root@localhost~]# vi ex1.py
[root@localhost~]# python ex1.py
please input yourusername:tony
input yourpassword:
the name is: tony
the password is:123
you loginsuccessfully!,welcome tony,super user
[root@localhost~]#
注:
1.冒號是條件控制的結束
2.同級縮進要一致:IndentationError2
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。