魅力python------if - else 語句

來源:互聯網
上載者:User

標籤:名稱   bsp   div   span   .com   AC   finish   大寫   控制台應用程式   

引入:if-else的作用,滿足一個條件做什麼,否則做什麼。

if-else語句文法結構

if 判斷條件:

要執行的代碼

else

要執行的代碼

 

判斷條件:一般為關聯運算式或bool類型的值

執行過程:程式運行到if處,首先判斷所帶的條件,如果條件成立,就是傳回值是True,則執行下面的代碼;如果條件不成立則傳回值是False, 則繼續執行下面的代碼。

 

樣本1:類比使用者登入

提示輸入使用者名稱和密碼

如果使用者名稱是Admin,密碼等於123.com, 提示使用者登入成功

如果使用者名稱不是Admin,提示使用者不存在

如果密碼不等於123.com, 提示密碼錯誤

username= input("請輸入使用者名稱:")

password = input("請輸入密碼:")

if username.lower().strip()== "admin" and password == "123.com":

    print("登入成功!")

else:

    print("使用者名稱或者密碼錯誤!")

# lower()--把字串轉為小寫 upper()--把字串轉為大寫

# strip()--去除字串前後的空格

 

結果:

C:\python\python.exeC:/python/demo/file2.py

請輸入使用者名稱:  ADMIN

請輸入密碼:123.com

登入成功!

 

Process finished with exit code 0

 

樣本2

在控制台應用程式中輸入小王(語文,英語,數學)成績(單科滿分100分)

判斷:

1)如果所有科目都及格了,提示:恭喜你,你所有科目都通過考試了

2)否則提醒:很遺憾,你沒有通過考試,需要補考(沒有及格的名稱)

chinese= int(input("請輸入語文成績:"))

maths = int(input("請輸入數學成績:"))

english = int(input("請輸入英語成績:"))

get_course = ""

if chinese>= 60 and maths >= 60 and english>= 60:

    print("恭喜你,所有科目都通過考試!")

else:

   if chinese <= 60:

        get_course += "語文、"

    if maths <= 60:

        get_course += "數學、"

    if english <= 60:

        get_course += "英語、"

    print("很遺憾,你沒有通過考試,補考科目為:+ get_course)

 

結果:

C:\python\python.exeC:/python/demo/file2.py

請輸入語文成績:54

請輸入數學成績:89

請輸入英語成績:32

很遺憾,你沒有通過考試,補考科目為:語文、英語、

 

Process finished with exit code 0

 

樣本3:(之前小紅花案例第一次最佳化)

在控制台應用程式中輸入小王(語文,英語,數學)成績(單科滿分100分)

判斷:

1)如果有一門是100分

2)如果有兩門大於90分

3)如果三門大於80分

滿足以上一種情況,則獎勵一朵小紅花

 

chinese= int(input("請輸入語文成績:"))

maths = int(input("請輸入數學成績:"))

englist = int(input("請輸入英語成績:"))

get_course = ""

if (chinese == 100 or maths == 100 or englist == 100):

    if(chinese == 100):get_course += "語文、"

    if(maths == 100):get_course += "數學、"

    if(englist == 100):get_course += "英語、"

    print("你的%s得了100分,獎勵一朵小紅花?% get_course)

else:

    if(chinese >= 90 and maths >=90) or (chinese >= 90 and englist>= 90) or(maths >= 90and englist >= 90):

     if(chinese >= 100):get_course += "語文、"

     if(maths >= 90):get_course += "數學、"

     if(englist >= 90):get_course += "英語、"

     print("你的%s大於90分,獎勵一朵小紅花?% get_course)

 else:

     if(chinese >= 80 and maths >=80 and englist >= 80):

     print("你的三個科目語文、數學、英語都大於80分,獎勵一朵小紅花?")

 

結果:

C:\python\python.exeC:/python/demo/file2.py

請輸入語文成績:86

請輸入數學成績:98

請輸入英語成績:87

你的三個科目語文、數學、英語都大於80分,獎勵一朵小紅花?

 

Process finished with exit code 0

 

魅力python------if - 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.