一、流程分支
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')