python入門二
一、流程控住之if判斷
語法1
if 條件:
代碼1
代碼2
代碼3】
...
cls=‘human‘
sex=‘female‘
age=18
if cls == ‘human‘ and sex == ‘female‘ and age > 16 and age < 22:
print(‘開始表白‘)
print(‘end....‘)
語法2
if 條件:
代碼1
代碼2
代碼3
...
else:
代碼1
代碼2
代碼3
...
cls=‘human‘
sex=‘female‘
age=38
if cls == ‘human‘ and sex == ‘female‘ and age > 16 and age < 22:
print(‘開始表白‘)
else:
print(‘阿姨好‘)
print(‘end....‘)
語法3
if 條件1:
代碼1
代碼2
代碼3
...
elif 條件2:
代碼1
代碼2
代碼3
...
elif 條件3:
代碼1
代碼2
代碼3
...
............
else:
代碼1
代碼2
代碼3
...
‘‘‘
如果:成績>=90,那麽:優秀
如果成績>=80且<90,那麽:良好
如果成績>=70且<80,那麽:普通
其他情況:很差
‘‘‘
score=input(‘your score: ‘) #score=‘73‘
score=int(score) #score=73
if score >= 90:
print(‘優秀‘)
elif score >= 80:
print(‘良好‘)
elif score >= 70:
print(‘普通‘)
else:
print(‘很差‘)
user_from_db=‘egon‘
pwd_from_db=‘123‘
user_from_inp=input(‘username>>>: ‘)
pwd_from_inp=input(‘password>>>: ‘)
if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:
print(‘login successfull‘)
else:
print(‘user or password error‘)
if的嵌套
cls=‘human‘
sex=‘female‘
age=18
is_success=False
if cls == ‘human‘ and sex == ‘female‘ and age > 16 and age < 22:
print(‘開始表白...‘)
if is_success:
print(‘在一起‘)
else:
print(‘我逗你玩呢....‘)
else:
print(‘阿姨好‘)
print(‘end....‘)
二、流程控制之while循環
while語法,while循環又稱為條件循環
while 條件:
code1
code2
code3
....
user_db=‘egon‘
pwd_db=‘123‘
while True:
inp_user=input(‘username>>: ‘)
inp_pwd=input(‘password>>: ‘)
if inp_user == user_db and inp_pwd == pwd_db:
print(‘login successfull‘)
else:
print(‘user or password error‘)
2 while+break:break的意思是終止掉當前層的循環,.執行其他代碼
while True:
print(‘1‘)
print(‘2‘)
break
print(‘3‘)
user_db=‘egon‘
pwd_db=‘123‘
while True:
inp_user=input(‘username>>: ‘)
inp_pwd=input(‘password>>: ‘)
if inp_user == user_db and inp_pwd == pwd_db:
print(‘login successfull‘)
break
else:
print(‘user or password error‘)
print(‘其他代碼‘)
3 while+continue:continue的意思是終止掉本次循環,.直接進入下一次循環
ps:記住continue一定不要加到循環體最後一步執行的代碼
n=1
while n <= 10: #
if n == 8:
n += 1 #n=9
continue
print(n)
n+=1 #n=11
while True:
if 條件1:
code1
code2
code3
continue #無意義
elif 條件1:
code1
continue #有意義
code2
code3
elif 條件1:
code1
code2
code3
continue #無意義
....
else:
code1
code2
code3
continue #無意義
while循環嵌套
user_db=‘egon‘
pwd_db=‘123‘
while True:
inp_user=input(‘username>>: ‘)
inp_pwd=input(‘password>>: ‘)
if inp_user == user_db and inp_pwd == pwd_db:
print(‘login successfull‘)
while True:
cmd=input(‘請輸入你要執行的命令: ‘)
if cmd == ‘q‘:
break
print(‘%s 功能執行...‘ %cmd)
break
else:
print(‘user or password error‘)
print(‘end....‘)
#while+tag
user_db=‘egon‘
pwd_db=‘123‘
tag=True
while tag:
inp_user=input(‘username>>: ‘)
inp_pwd=input(‘password>>: ‘)
if inp_user == user_db and inp_pwd == pwd_db:
print(‘login successfull‘)
while tag:
cmd=input(‘請輸入你要執行的命令: ‘)
if cmd == ‘q‘:
tag=False
else:
print(‘%s 功能執行...‘ %cmd)
else:
print(‘user or password error‘)
print(‘end....‘)
#while+else (***)
n=1
while n < 5:
# if n == 3:
# break
print(n)
n+=1
else:
print(‘在整個循環結束後,會進行判斷:只有while循環在沒有被break結束掉的情況下才會執行else中的代碼‘)
三、流程控制之for循環
names=[‘egon‘,‘asb‘,‘wsb‘,‘lsb‘,‘csb‘]
n=0
while n < len(names):
print(names[n])
n+=1
names=[‘egon‘,‘asb‘,‘wsb‘,‘lsb‘,‘csb‘]
info={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}
for k in info: #x=‘‘age‘
print(k,info[k])
# for item in names:
# print(item)
for i in range(1,10):
print(i)
for i in range(10): #默認的起始位置是0
print(i)
for i in range(1,10,2): #1 3 5 7 9
print(i)
names=[‘egon‘,‘asb‘,‘wsb‘,‘lsb‘,‘csb‘]
for i in range(len(names)):
print(i,names[i])
for i in range(5):
print(‘========>第一層: %s<=========‘ %i)
for j in range(3):
print(‘ 第二層: %s‘ %j)
for+break
names=[‘asb‘,‘wsb‘,‘egon‘,‘lsb‘,‘csb‘]
for n in names:
if n == ‘egon‘:
break
print(n)
for+continue
names=[‘asb‘,‘wsb‘,‘egon‘,‘lsb‘,‘csb‘]
for n in names:
if n == ‘egon‘:
continue
print(n)
#for+else
names=[‘asb‘,‘wsb‘,‘egon‘,‘lsb‘,‘csb‘]
for n in names:
# if n == ‘egon‘:
# break
print(n)
else:
print(‘=====>‘)
python入門二