Fifth, the process control value if ... else

1.1, process control

  That flow control flow control, flow control refers to the execution of the program, and the flow control execution process is divided into three structures: the structure of the order (from top to bottom, in sequence run), a branched structure (if a judgment), the cyclic structure ( and judgment while for).

1.2, the branch structure

  Is a branched structure to perform different branches depending on the conditions corresponding to the subcode judgment is true.

1.3 Why branch structure?

  In some cases humans need to decide what to do based on conditions such as: rain today, need an umbrella.

  It requires a computer that also have the ability to judge. Therefore, the program must have a mechanism to control the corresponding computer.

1.4, if the syntax

  Use keywords to be implemented if the branch structure, complete syntax is as follows:

  if Condition 1:

   Code 1

   .......

  elif Condition 2:

   Code 2

   ......

  else condition 3:

   ......

note:

  1.Python same indentation (4 identifies an indented spaces) to identify a set of code blocks, the unified code run by the order from the bottom.

  2. The condition can be any expression, but the results must be a boolean type.

  3. All data types will be automatically converted into Boolean if the determination.

1.5 Exercises

1.5.1 If a woman's age> 30 years old, then: call aunt

age _of_girl = 31

if age_of_girl>30:

  print ( 'good aunt')

1.5.2 If a woman's age> 30 years old, then: call aunt, otherwise: a lady

age_of_girl =18

if age_of_girl > 30:

  print ( 'good aunt')

else:

  print ( 'good lady')

1.5.3 If a woman's age> = 18 years and <= 22, and height> 170, weight <100, and is beautiful, then tell the truth, otherwise called aunt.

age_of_girl =19

height = 171

weight = 98

is_pretty = True

if age_of_girl >=18 and age_of_girl <22 and height>170 and weight <100 and is_bretty==True:

  print ( 'express ...')

else:

  print ( 'good aunt')

1.5.4, on the basis of the confession continued:

           If the confession is successful, then together:

           Otherwise: print ......

age_of_girl =19

height =171

weight =99

pretty =True

success =Flase

if age_of_girl >=18 and age_of_girl <22 and height >170 and weight<100 and pretty = True:

  if success =True:

    print ( 'declare success ...')

  else:

    print ( 'confession of failure ...')

else:

  print ( 'good aunt')

1.5.5 if: score> = 90, then: Excellent

      If: score> = 80 and <90, then: good

           If: score> = 70 and <80, then: general

      If: score> = 60 and <70, then: pass

           Other cases, they failed.

score = input ( 'Please enter your score:')

score=int(score)

if score>=90:

  print ( 'excellent')

elif score>=80:

  print ( 'good')

elif score>=70:

  print ( 'Normal')

elif score >=60:

  print ( 'pass')

else:

  print ( 'fail')

1.5.6, user login authentication

name = input ( 'Enter Username:')

password = input ( 'Enter password:')

if name='egon' and password = 123:

  print ( 'successful landing')

else:

  print ( 'Login failed')

1.5.7, and outputs the input content in accordance with user privileges

'''

egon ====> Super Admin

tom ====> general administrator

jack, rain ====> Head

Other ==== "ordinary users

'''

name = input ( 'Please enter the name:')

if name=='egon':

  print ( 'Super Admin')

elif name=='tom':

  print ( 'general administrator')

elif name =='jack' or name == 'rain':

  print ( 'Head')

else:

  print ( 'normal user')

1.5.8、

'' ' 

  If today is Monday, then: go to work

  if today is Tuesday, then: go to work

  if today is Wednesday, then: go to work

  if today is Thursday, then: go to work

  if today is Friday, then: go to work

  if today is Saturday, then: Long out

  if today is Sunday, then: go out waves

' ''

taday the INPUT = ( 'What day is today:')

IF taday == 'catalog on Monday':
Print ( 'to go to work today')
elif taday == 'catalog on Tuesday':
Print ( 'go to work today')
elif taday == 'catalog on Wednesday':
Print ( 'to go to work today')
elif taday == 'catalog on Thursday':
Print ( 'to go to work today')
elif taday == 'catalog on Friday':
Print ( 'to go to work today')
taday == elif 'Saturday':
Print ( 'out of the waves')
elif taday ==' Sunday ':
Print ('Out waves')
the else:
Print (
'' 'Enter one: 
catalog on Monday
on Tuesday
catalog on Wednesday
Turesday
catalog on Friday
Saturday
the Sunday' ''
)

Guess you like

Origin www.cnblogs.com/jingpeng/p/12393025.html