首頁  >  文章  >  後端開發  >  python中if 條件判斷程式碼解析

python中if 條件判斷程式碼解析

不言
不言原創
2018-09-20 16:12:362256瀏覽

這篇文章帶給大家的內容是關於python中if 條件判斷程式碼解析,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

條件語句的執行程序:

if 條件判斷注意:
1.每個條件後面要使用冒號: ,表示條件為True時要執行的程式碼;
2.使用縮排來劃分程式碼區塊,相同縮排數的語句在一起組成一個程式碼區塊。

if...else,單一條件判斷

#
username_store = 'lipandeng'
password_store = '123'

username_input = input('your username:')
password_input = input('your password:')

if username_input == username_store and password_input == password_input:
    print('welcome {0}'.format(username_input))
else:
    print('Invalid username and password!')

if...elif.. .else,多條件判斷

score = int(input('Enter your score:'))     # input()返回的数据类型是str,int()函数是把str转int。

if score < 0 or score > 100:    # 条件1,此条件为True时执行print(),elif后面的代码不再执行。
    print(&#39;Scores of the range is 0-100.&#39;)
elif score >= 90:   # 条件2,当条件1为False时判断条件2,此条件为True时执行print(),elif后面的代码不再执行。
    print(&#39;Your score is excellent.&#39;)
elif score >= 60:   # 条件3,当条件1和条件2为False时判断条件3,此条件为True时后执行print(),elif后面的代码不再执行。
    print(&#39;Your score is good.&#39;)
else:   # 条件4,以上判断条件都为False时执行的print()。
    print(&#39;Your score is not pass the exam.&#39;)

以上是python中if 條件判斷程式碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn