Python- conditional branch

Python- conditional branch

First, the simple conditional branch

  1. Conditional branch: The return value is either true or false
  2. Statement: IF-elif-elif-the else

if ( condition A): Statement A

elif ( Condition B): Statement B

elif ( Conditions C): Statement C

else: statement D

Wherein: elif equivalent else if

note:

1)  behind each condition where a colon  :

2) Python without braces { distinguished using indentation divided statement blocks, the number of statements in the same indentation together to form a block of statements

3) Python does not switch - case statement

  1. Example:

age = int (input ( "input older input test:"))

print("")

if age < 0:

    print ( "You're still stomach it? actually less than zero!")

elif age == 1:

    print ( "equivalent to 14 years of age.")

elif age == 2:

    print ( "equivalent to 22 years of age.")

elif age > 2:

    human = 22 + (age -2)*5

print ( "corresponding to the human ages:", human)

else:

print ( "input error!")

 

  1. Exercise: size ratio (randomly 0-100 generate a number, the number of correct size comparison)

GussNumber=random.randint(0,100)

InputNumber = int (intput ( "Please enter the number you guess:"))

If(GussNumber<InputNumber):

print ( "Your guess is too big!")

elif (GussNumber == InputNumber):

print ( "You guessed it!")

else:

print ( "Your guess is too small!")

Second, the nested conditional branch

  1. Nesting of: if ... elif ... else structure in another if ... elif ... else structure.
  2. Statement:

if ( condition A): Statement A

if ( condition a): Statement a

elif ( condition b): Statement b

elif ( condition c): statement c

else: statements d

elif ( Condition B): Statement B

if ( condition a): Statement a

elif ( condition b): Statement b

elif ( condition c): statement c

else: statements d

elif ( Conditions C): Statement C

else: statement D

  1. Exercise: check grades (grades input, view level)

InputNumber = int (intput ( "Please enter your score:"))

If(InputNumber<0 or InputNumber>100):

print ( "What the hell, what you are going to heaven! There is no hierarchy!")

elif (InputNumber = 0):

print ( "I'm sorry, you got two duck!")

else:

If(InputNumber<60):

print ( " ah, got two fail , grade D")

elif (InputNumber <75):

print ( " Congratulations, you passed the test! Level C")

elif (InputNumber <85):

print ( " Congratulations, you're a good boy! Level B")

else:

print ( " Wow, cow! grade A")

 

Guess you like

Origin www.cnblogs.com/sulanyuan/p/11202607.html