Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use if statement in python

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use if sentences in python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

If statement: judge the statement and judge whether to continue execution according to the condition

Enter:

#! / usr/bin/python

# Filename: if.py

Number = 23

Guess = int (input ('Enter an integer:'))

If guess = = number:

Print ('Congratulations, you guessed it.')

# New block starts here

Print ('(but you do not win any prizes!)')

# New block ends here

Elif guess

< number: print('No, it is a little higher than that') # Another block # You can do whatever you want in a block ... else: print('No, it is a little lower than that') # you must have guess >

Number to reach here

Print ('Done')

# This last statement is always executed, after the if statement is executed

Run the output for the first time:

$python if.py

Enter an integer: 50

No, it is a little lower than that

Done

Run the output for the second time:

$python if.py

Enter an integer: 22

No, it is a little higher than that

Done

Run the output for the third time:

$python if.py

Enter an integer: 23

Congratulations, you guessed it. (but you do not win any prizes!)

Done

Explanation:

The above execution code is a number-guessing Mini Game. After running the program, the user is prompted to enter a number, and the user performs the number guess by entering a number in the console.

The if statement serves as a judgment here, prompting you to guess correctly if the condition is met (that is, the guessed numbers are equal).

If the condition is not satisfied, the branch statement elif of the if condition will be executed

Level 2 judgment: the guessed number is too small

If the condition is still not met, the branch statement of the if condition continues to execute the elif,3 level judgment: the guessed number is too large

All the conditions that can be judged have been executed, and the result is output.

Enter a number to determine the if condition, and there will be only one output: either equal prompt, guess right, big or small.

Enter:

If True:

Print ('Yes, it is true')

Output:

Yes, it is true

Explanation:

If statement structure

If condition:

Print ("content")

Notice the colon above and the indentation of the statement after the colon.

After reading the above, do you have any further understanding of how to use if statements in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report