流程控制

发布时间:2020-06-15 18:15:02 作者:huwei0512
来源:网络 阅读:322

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


推荐阅读:
  1. mysql 流程控制函数
  2. 8. 流程控制

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

流程控制 同级 缩进一致

上一篇:VMware Workstation克隆虚拟机

下一篇:OpenLayers的使用---- 一个完全免费开源的地图JS库

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》