A, if determined
Sims to judge certain things and the ability to make different decisions. Due to humanlike computer to work, it must also have the ability to judge right and wrong things, in order to make a different response. Examples of practice, and walked in front of a sister paper, you will want a good look good, not to exceed a positive face to look like. Programs such as ATM machines, you need to receive input user name and password to determine whether you are a legitimate user, and so on.
Syntax: if conditions:
Code 1
Code 2
Code 3
...
else:
Code 1
Code 2
Code 3
...
if ... else ... the establishment of a code indicating what will happen, else said they did not set up what will happen.
cls = 'human' gender = 'female' age = 18 if cls == 'human'and 'gender' == 'female' and age >16 and age < 22: print('上去表白') else: print(‘打扰了’')
if...elif...
if Condition 1:
Code 1
Code 2
Code 3
...
elif Condition 2:
Code 1
Code 2
Code 3
...
elif Condition 3:
Code 1
Code 2
Code 3
...
...
the else:
Code 1
Code 2
Code 3
...
if ... elif ... else represent if conditions do 1 set up, elif condition 2 set up to do, elif condition 3 set up to do, elif ... else to do.
if...elif...else
cls = 'human'
gender = 'female'
age = 28
CLS == IF 'Human' and Gender == 'FEMALE' and Age> 16 Age and <22 is:
Print ( 'start declare')
elif CLS == 'Human' and Gender == 'FEMALE' and Age> Age and 22 is <30:
Print ( 'consider')
the else:
Print ( 'good aunt')
Application examples: # Emulating Register Log in
user_name = 'zzj'
user_pwd = '123'
inp_name = input('please input your name>>>:')
inp_pwd = input('please input your password>>>:')
if user_name == inp_name and userpwd == inp_pwd:
print ( 'successful landing!')
else :
print ( 'login error!')
# One week arrangements
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday Sunday
''')
today=input('>>: ')
if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
print('上班')
elif today == 'Saturday' or today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
if nesting
cls = 'human'
gender = 'female'
age = 18
is_success = False
CLS == IF 'Human' and Gender == 'FEMALE' and Age> 16 and Age <22:
Print ( 'the beginning declare')
IF is_success:
Print ( 'that we go together ...')
the else:
Print ( 'I tease you play with')
the else:
Print ( 'good aunt')
Two, while circulation
Realization of ATM to re-enter the password function
user_db = 'nick'
pwd_db = ‘123’
while True:
inp_user = input('username:')
inp_pwd = input('password:')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
else:
print('username or password error')
while + break
break out of the cycle, which means that the termination of the current layer, execute other code.
#break statement presentation
while True :
print('1')
print('2')
print('3')
break
Example:
user_db = 'jason'
pwd_db = '123'
while True:
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
break
else:
print('username or password error')
print ( 'out of the while loop')
#while + else
n = 1
while n < 3:
if n == 2:
print(n)
n +=1
else:
ptint ( 'else will only execute the code in the else while when no break')
Three, for circulation
1. for i in range (1,10): #range care regardless tail
print(i)
2.name_list = ['jason','zzj','angela']
for i in range(len(name_list)):
prtint(i,name_list[i])
3.for + break out of this loop layer
name = ['jason','zzj','angela']
for i in name:
if i == 'zzj':
break
print(i)
4.for + continue
name = ['jason','zzj','angela']
for i in name:
if i == 'zzj':
continue
print(i)
5.for circulation exercises
Print Jiujiushengfa formulas table
for i in range(1,10): for j in range(1,i+1): print('%s*%s=%s'%(i,j,i*j),end=' ') print()