Analyzing then determine primary Python 5 (b)

review:

Branch : completion of the test and is called the branch judgment based on the results.

Block : the line or lines of code together

Indent : a line of code a little bit to the right

Relational operator (comparison operator): ==,>,> =, <, <=, =,!

First, other types of tests

>, >=

Exercise 1:

n1 = int(input("please input first num: "))
n2 = int(input("please input second num: "))
if n1 > n2:
    print(n1, ">", n2)
else:
    print(n1, "<=", n2)

Exercise 2:

import random
a = int(random.uniform(0, 3))
if a >=1:
    print("a >= 1, a=", a)
else:
    print("a=", a)

 

Second, a plurality of test conditions

1, and

Suppose to play a game that requires two conditions: 1 at least 8 years old, at least 2 third grade

if age >= 8:
    pass
if grade >= 3:
    pass

image

 

if age >= 8 and grade >=3:
    print("you can play.")

 

2, or

Suppose a game, as long as the kids enjoy one of three colors, you can play: 1 red, 2 blue, 3 green

if color == "red":
    print("you can play.")
if color == "blue":
    print("you can play.")
if color == "green":
    print("you can play.")

image

 

3, using not

Can be used to compare not reversed, it represents the opposite logic

if a >= 1:
    pass

Can be written not a <1

if not a < 1:
    pass

Logical operators : and, or, not

Guess you like

Origin www.cnblogs.com/luhouxiang/p/11371863.html