Python——if、elif、else語句

來源:互聯網
上載者:User

標籤:IV   str   else   分支   16px   lov   一起   div   年齡   

一、流程分支 

    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=253 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‘)

Python——if、elif、else語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.