码迷,mamicode.com
首页 > 其他好文 > 详细

流程控制

时间:2017-03-09 01:39:04      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:流程控制 同级缩进一致 冒号是条件控制的结束

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


流程控制

标签:流程控制 同级缩进一致 冒号是条件控制的结束

原文地址:http://2889688.blog.51cto.com/2879688/1904363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!