target
-
Conditional statement function
-
if syntax
-
if...else...
-
Multiple judgment
-
if nesting
I Understanding conditional statements
Suppose a scenario:
-
Have you ever been to an Internet cafe at this age?
-
What is the one thing you must do when you want to surf the Internet? (key considerations)
-
Why give the ID card to the staff?
-
Is it to judge whether you are an adult?
-
If adults can surf the Internet? If you are not an adult, you are not allowed to surf the Internet?
In fact, the so-called judgment here is a conditional statement, that is, if the condition is true, some codes will be executed, and if the condition is not true, these codes will not be executed.
II if syntax
2.1 syntax
if condition: Code 1 for conditional execution Code 2 for conditional execution ......
2.2 quick experience
if True: print('Code 1 for conditional execution') print('Code 2 for conditional execution') # The following code is not indented into the if statement block, so it has nothing to do with the if condition print('I am the code to execute whether the condition is true or not')
The results are as follows:
III Example: Internet access
Demand analysis: if the user is older than or equal to 18 years old, i.e. adult, the output is "adult, you can surf the Internet".
3.1 simple version
age = 20 if age >= 18: print('You are an adult and can surf the Internet') print('System shutdown')
3.2 advanced version
New requirements: users can output their own age, and then the system will judge whether they are adults. When they are adults, they will output "your age is' the age entered by users', and you can surf the Internet".
# input accepts that the data entered by the user is of string type, provided that age and integer 18 make judgment, so the data type should be converted to int here age = int(input('Please enter your age:')) if age >= 18: print(f'What is your age{age},He is an adult and can surf the Internet') print('System shutdown')
IV if...else...
Function: if the condition is true, execute the code below if; If the condition does not hold, execute the code below else.
Thinking: Internet cafes are examples of surfing the Internet. If you are an adult, you are allowed to surf the Internet. What if you are not an adult? Should I reply that users can't surf the Internet?
4.1 syntax
if condition: Code 1 for conditional execution Code 2 for conditional execution ...... else: Code 1 executed when the condition is not true Code 2 executed when the condition is not true ......
4.2 practical version: Internet cafes
age = int(input('Please enter your age:')) if age >= 18: print(f'What is your age{age},He is an adult and can surf the Internet') else: print(f'What is your age{age},Minors, please go home and do your homework by yourself') print('System shutdown')
Note: if the condition is true and some code is executed, the code in other cases will not be executed.
5, Multiple judgment
Thinking: the legal working age in China is 18-60 years old, that is, if the age is less than 18, it is child labor, which is illegal; If the age is between 18-60 years old, it is legal length of service; More than 60 years old is the legal retirement age.
5.1 syntax
if Condition 1: Code 1 executed when condition 1 is true Code 2 executed when condition 1 is true ...... elif Condition 2: Code 1 executed when condition 2 is true Code 2 executed when condition 2 is true ...... ...... else: None of the above conditions is true. Execute the code
Multiple judgments can also be used with else. Generally, else is placed at the end of the entire if statement to represent the code executed when the above conditions are not true.
5.2 example: length of service judgment
age = int(input('Please enter your age:')) if age < 18: print(f'What is your age{age},One for child labor') elif (age >= 18) and (age <= 60): print(f'What is your age{age},Legal length of service') elif age > 60: print(f'What is your age{age},Can retire')
Expansion: age > = 18 and age < = 60 can be simplified to 18 < = age < = 60.
6, if nesting
Thinking: take the bus: if you have money, you can get on the bus, but if you don't have money, you can't get on the bus; If there is an empty seat after getting on the bus, you can sit down; If you don't have a seat available, stand. How to write a program?
6.1 syntax
if Condition 1: Code executed when condition 1 is true Code executed when condition 1 is true if Condition 2: Code executed when condition 2 is true Code executed when condition 2 is true
Note: the if of condition 2 is also inside the indentation relationship of the code executed when condition 1 is established.
6.2 example: taking bus
6.2. 1 judge whether you can get on the bus
""" 1. If you have money, you can get on the bus 2. After getting on the bus, if you have a seat available, you can sit down After getting on the bus, if there is no empty seat, stand and wait for an empty seat If you don't have money, you can't get on the bus """ # Suppose money = 1 means money and money = 0 means no money money = 1 if money == 1: print('Local tyrant, not bad money, get on the bus smoothly') else: print('No money, can't get on the bus and run after the bus')
6.2. 2 judge whether you can sit down
""" 1. If you have money, you can get on the bus 2. After getting on the bus, if you have a seat available, you can sit down After getting on the bus, if there is no empty seat, stand and wait for an empty seat If you don't have money, you can't get on the bus """ # Suppose money = 1 means money, and money = 0 means no money; seat = 1 means there is an empty seat, and seat = 0 means there is no empty seat money = 1 seat = 0 if money == 1: print('Local tyrant, not bad money, get on the bus smoothly') if seat == 1: print('If you have a seat available, you can sit down') else: print('No empty seats, stations, etc') else: print('No money, can't get on the bus and run after the bus')
6.3 if nested execution process
VII Application: guessing game
Demand analysis:
-
Characters participating in the game
-
game player
-
Manual punch
-
-
computer
-
Random punch
-
-
-
Judge whether to win or lose
-
Player wins
game player computer stone scissors scissors cloth cloth stone -
it ends in a draw
-
Player punches are the same as computer punches
-
-
Computer wins
-
Random method:
-
Export random module
-
random. RandInt (start, end)
""" Tip: 0-Stone, 1-Scissors, 2-cloth 1. punches Player input punch Computer random punch 2. Judge whether to win or lose Player wins it ends in a draw Computer wins """ # Import random module import random # Calculate the random number of the fist produced by the computer computer = random.randint(0, 2) print(computer) player = int(input('Please punch: 0-Stone, 1-Scissors, 2-Cloth:')) # Player wins p0:c1 or p1:c2 or p2:c0 if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0): print('Player wins') # Draw: player = = computer elif player == computer: print('it ends in a draw') else: print('Computer wins')
VIII ternary operator
Ternary operators are also called ternary operators.
The syntax is as follows:
Value 1 if condition else Value 2
Quick experience:
a = 1 b = 2 c = a if a > b else b print(c) #2
summary
-
if statement syntax
if condition: Code executed when condition is satisfied
-
if...else...
if condition: Code executed when condition is satisfied else: Code executed when the condition is not true
-
Multiple judgment
if condition 1: Code executed when condition 1 is true elif condition 2: Code executed when condition 2 is true else: None of the above conditions holds for the executed code
-
if nesting
if condition 1: Code executed when condition 1 is true if condition 2: Code executed when condition 2 is true ....