Home  >  Article  >  Backend Development  >  A brief introduction to the If statement and While statement in Python (with examples)

A brief introduction to the If statement and While statement in Python (with examples)

不言
不言Original
2018-09-25 16:34:113287browse

This article brings you a brief introduction to the If statement and While statement in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

If statement

is used to check the condition: If the condition is true (True), it will run This block of statements (called if-block or if block)

    will run another block of statements (called else-block or else block), in which the else clause is optional

Multi-branch if statements: 1, elif and else also need to have a colon at the end of their logical lines, followed by their corresponding statements Block

##       2. You can set another if statement (nested if statement) within an if statement of the if block

数字猜测:
       
number = 50
guess_number = input("请输入猜测的数字:")
# input() 函数将以字符串的形式返回我们所输入的内容

if int(guess_number) == number :
         #if块从此开始
         print("恭喜你!猜对了!")
elif int(guess_number) < number :
         # 另一代码块开始
         print("很抱歉,你猜的小了!")
else :
         print("很抱歉,你猜的大了!")

While Statement

The While statement allows you to repeatedly execute a block of statements while a condition is true. The while statement is a type of looping statement. The while statement can also have an else clause as an optional option.

Set the variable running to True before the while loop begins. When the program starts, it first checks whether the variable running is True, and then executes the corresponding while block. After this code block is executed, the condition will be rechecked. If the variable is still True , the program will execute the while block again, otherwise it will continue executing the optional else block and move to the next statement.

The Else code block begins executing when the while loop's condition becomes False, possibly even when the condition is checked for the first time. If there is an else block within a while loop, it will always be tried unless the loop is interrupted by a break statement.

number = 50
max = 100
# input( ) 函数将以字符串的形式返回我们所输入的内容
running = True
while running :
        guess_number = input("请输入猜测的数字:")
        if int(guess_number) == number :
        # if 块从此开始
                print("恭喜你!猜对了!")
                #这将导致 while 循环终止
                running = False
        elif int(guess_number) < number :
        # 另一代码块
                print("很抱歉,你猜小了!")
                print("这个数字是在" + str(guess_number) + "和" + str(max) + "之间")
                min = guess_number
                
        else :
                print("很抱歉,你猜大了!")
                print("这个数字是在" + str(min) + "和" + str(guess_number) + "之间")
                max = guess_number
                
print(&#39;游戏结束!&#39;)

The above is the detailed content of A brief introduction to the If statement and While statement in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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