1.if conditional statement
Example 1: (Separate if use) The
running result is as follows: because age is less than 18, if condition is not established, so the content in the condition is not executed, and the content outside the condition can still be executed.
Example 2: (use if and else together)
Example 3: (joint use of if and elif, else)
Example 4: (nested use of if and else)
2. Logical operators
And (
or)
not (not)
Example 1:
Example 2:
Note: The if judgment statement is followed by a bool value. When the bool value is True, the condition is established; when the bool value is False, the condition is not established. The three concepts of null, 0, and False can be understood in the same way for judging bool values.
So example 2 can be modified as follows:
3.if a comprehensive test
Supplementary knowledge:
In python, to use random numbers, you first need to import the random number module (toolkit). After importing the module, you can directly type an English dot after the module name, and then the Tab key will prompt all functions contained in the module .
random.randint (a, b) returns the integer between [a, b], including a and b.
eg: random.randint (12,20): generate random number n: 12 <= n <= 20
random.randint (20,20): the result will always be 20
random.randint (20,12): the result will report an error: lower limit Must be less than the upper limit.
Question stem:
1. From the console input to a boxing - a stone (1) / scissors (2) / fabric (3)
2. Computer random punches - only the first computer assumes that the stones, to complete the overall function of the code
3. Comparative wins Negative:
Rock wins scissors
Scissors wins cloth
Cloth wins stones
Problem-solving steps: The
final output is as follows: