流程控制
admin
2023-07-31 16:22:16
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


相关内容

热门资讯

美军:新一轮对伊打击已完成,命... 美伊冲突再起,美国对伊朗发动新一轮打击,并击中了80多个目标。伊朗指责美国违反两国此前签署的谅解备忘...
中国“宫斗爽游”,让韩国人集体... 韩国的年轻人,正在疯狂“上头”一款来自中国的游戏:《盛世天下》。这款互动影游,主打“真人演出+宫廷生...
19岁立遗嘱留给“发小”200... “因为经常参加户外极限运动,担心发生人身意外,所以想来立个遗嘱,可以由我指定的继承人来处理或继承遗产...
日菲军事勾连,东南亚民众深感不... 【环球时报驻菲律宾特派记者 樊帆 环球时报记者 陈子帅 李艾鑫】编者的话:7月5日,菲律宾总统马科斯...
遭龙卷风袭击后,湖北黄冈首批1... 澎湃新闻记者 吕新文7月7日,在湖北省黄冈市黄州区东湖街道受灾居民区,救援人员帮助居民返回房屋拿取贵...
最大13级风力!罕见龙卷风突袭... 谁也没有料到,一场巨大的龙卷风竟横扫长江两岸,让湖北一夜无眠。7月6日晚,闪电劈开黑夜,狂风暴雨席卷...
新华每日电讯:“下个雨能多大”... 近年来,打破以往规律、超越传统认知的极端天气在我国明显增多。沙漠会发洪水吗?2026年6月的塔克拉玛...
终止豁免!美撤销对伊朗石油销售... △美国财政部(资料图)根据美国财政部外国资产控制办公室当地时间7月7日消息,美国撤销一项授权允许伊朗...
《菲律宾南海领土主张的历史法理... 7月8日上午,自然资源部海洋发展战略研究所发布了《菲律宾南海领土主张的历史法理批判》报告。《报告》回...
台风“巴威”正向中国靠近,预计... 今天(7月8日)早晨5点,今年第9号台风“巴威”的中心位于台湾基隆市东偏南方向约1650公里的洋面上...