day-4,流程控制之if判斷,while循環,for循環

1流程控制之if判斷

if   若是         elif  那麼           else 其他ide

# 語法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....')
if判斷

 

2 流程控制之while循環

while循環又稱爲條件循環spa

while  +  條件: code

while+break:break的意思是終止掉當前層的循環,執行其餘代碼blog

while+continue:continue的意思是終止掉本次循環,直接進入下一次循環input

  continue必定不要加到循環體最後一步執行的代碼cmd

while+else: else(其他)只有在整個循環結束後,纔會進行判斷;只有while循環在沒有被break結束掉的狀況下才會執行else中的代碼for循環

 

#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....')
# 語法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
#     ...




user_from_db='egon'
pwd_from_db='123'

user_from_inp=input('username>>>: '#while+else (***)
n=1
while n < 5: # if n == 3:
    # break
    print(n) n+=1
else: print('在整個循環結束後,會進行判斷:只有while循環在沒有被break結束掉的狀況下才會執行else中的代碼')
while循環

3 流程控制之for循環

len的意思是統計長度    for x in range          for  x in range +break    for x in range+continue              for x in range+else  event

for循環
相關文章
相關標籤/搜索