python-if condition judgment statement

1.if conditional statement

Example 1: (Separate if use) The
Insert picture description here
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.
Insert picture description here
Example 2: (use if and else together)
Insert picture description here
Example 3: (joint use of if and elif, else)
Insert picture description here
Example 4: (nested use of if and else)
Insert picture description here
Insert picture description here

2. Logical operators

And (
or)
not (not)

Example 1:
Insert picture description here
Example 2:
Insert picture description here

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:
Insert picture description here

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
Insert picture description here
Insert picture description here
final output is as follows:
Insert picture description here

Published 41 original articles · praised 0 · visits 1695

Guess you like

Origin blog.csdn.net/qq_44749796/article/details/105608457