Home > Backend Development > Python Tutorial > Analysis of if conditional judgment code in python

Analysis of if conditional judgment code in python

不言
Release: 2018-09-20 16:12:36
Original
2318 people have browsed it

The content of this article is about the analysis of if conditional judgment code in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Execution process of conditional statement:

if condition Judgment Note:
1. Use a colon after each condition : to indicate the code to be executed when the condition is True;
2. Use indentation to divide code blocks. Statements with the same indentation number form a code block together.

if...else, single condition judgment

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!')
Copy after login

if...elif.. .else, multi-condition judgment

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;)
Copy after login

The above is the detailed content of Analysis of if conditional judgment code in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template