if 条件判断

发布时间:2019-03-12 22:59:43编辑:auto阅读(1876)

    条件语句的执行过程:

    if 条件判断注意:
    1.每个条件后面要使用冒号 : ,表示条件为True时要执行的代码;
    2.使用缩进来划分代码块,相同缩进数的语句在一起组成一个代码块。

    if...else,单条件判断

     1 username_store = 'lipandeng'
     2 password_store = '123'
     3 
     4 username_input = input('your username:')
     5 password_input = input('your password:')
     6 
     7 if username_input == username_store and password_input == password_input:
     8     print('welcome {0}'.format(username_input))
     9 else:
    10     print('Invalid username and password!')

    if...elif...else,多条件判断

     1 score = int(input('Enter your score:'))     # input()返回的数据类型是str,int()函数是把str转int。
     2 
     3 if score < 0 or score > 100:    # 条件1,此条件为True时执行print(),elif后面的代码不再执行。
     4     print('Scores of the range is 0-100.')
     5 elif score >= 90:   # 条件2,当条件1为False时判断条件2,此条件为True时执行print(),elif后面的代码不再执行。
     6     print('Your score is excellent.')
     7 elif score >= 60:   # 条件3,当条件1和条件2为False时判断条件3,此条件为True时后执行print(),elif后面的代码不再执行。
     8     print('Your score is good.')
     9 else:   # 条件4,以上判断条件都为False时执行的print()。
    10     print('Your score is not pass the exam.')

     

关键字