Python——if、elif、else语句

一、流程分支 

    1. 单分支:

      例1:建立一个判断,输入年龄,判断年龄是否大于23,如果大于23,打印结果:‘It is time to find a bf’:

   

1 age_of _you=int(input('please input your name:'))
2 age_of _you=25
3 if age_of _you > 23:
4     print('it is time to find a bf)

    2.双分支:

    例2:输入用户名和密码,当用户名和密码都正确时欢迎用户,否则提示错误

  

 1 _username='shanshan'
 2 _password='123456'
 3 
 4 username=input('pleaue input your name:')
 5 password=input('pleaue input your password:')
 6 
 7 if _username==username and _password==password:
 8     print('welcome',username)
 9 else:
10     print('worong username or password')

  3.分支下继续分支

  例3:输入年龄,性别,名字,如果是女生,年龄<28,输出‘I love girls’,年龄>28,输出姐弟恋也很好哦,如果是男生,输出一起来搞基吧

  

 1 name=input('name:')
 2 sex=input('sex:')
 3 age=int(input("age:"))
 4 
 5 if sex=='f':
 6     if age<28:
 7         print('I love girls')
 8     else:
 9         print('姐弟恋也很好哦')
10 else:
11     print('一起来搞基吧')

 4. 多分支

 例4.练习:对成绩等级,A:90~100,B:80~89,C:60~79,D:40~59,E:<39

 1 grade=int(input('grade:'))
 2 
 3 if grade>=90:
 4     print('A')
 5 elif grade>=80:
 6     print('B')
 7 elif grade>=60:
 8     print('C')
 9 elif grade>=40:
10     print('D')
11 else:
12     print('E')

猜你喜欢

转载自www.cnblogs.com/xwxs/p/9193420.html