經常使用邏輯 if、while、for

1、if 判斷

語法一:

     if 條件:
     # 條件成立時執行的子代碼塊
         代碼1
         代碼2
         代碼3數組

示例:spa

sex='female'
age=18
is_beautiful=True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:
    print('開始表白。。。')

print('other code1...')
print('other code2...')
print('other code3...')
 

語法二:

     if 條件:
         # 條件成立時執行的子代碼塊
         代碼1
         代碼2
         代碼3
     else:
         # 條件不成立時執行的子代碼塊
         代碼1
         代碼2
         代碼3code

示例:blog

sex='female'
age=38
is_beautiful=True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:
    print('開始表白。。。')
else:
    print('阿姨好。。。')


print('other code1...')
print('other code2...')
print('other code3...')
 

語法三:

     if 條件1:
         if 條件2:
             代碼1
             代碼2
             代碼3input

sex='female'
age=18
is_beautiful=True
is_successful=True
height=1.70

if sex == 'female' and age > 16 and age < 20 and is_beautiful \
        and height > 1.60 and height < 1.80: 
    print('開始表白。。。')
    if is_successful:
        print('在一塊兒。。。')
    else:
        print('什麼愛情不愛情的,愛nmlgb的愛情,愛nmlg啊.')
else:
    print('阿姨好。。。')


print('other code1...')
print('other code2...')
print('other code3...')
 

語法四:

     if 條件1:
         代碼1
         代碼2
         代碼3
     elif 條件2:
         代碼1
         代碼2
         代碼3
     elif 條件3:
         代碼1
         代碼2
         代碼3for循環

        ……class

      else:循環

        代碼1語法

        代碼2im

        代碼3

示例:
若是成績 >= 90,那麼:優秀

若是成績 >= 80且 < 90, 那麼:良好

若是成績 >= 70且 < 80, 那麼:普通

其餘狀況:不好

score = input('please input your score: ')  # score='100'
score = int(score)

if score >= 90:
    print('優秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('不好')

    

二 、while循環

語法一:

     while 條件:
         代碼1
         代碼2
         代碼3

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
    else:
        print('username or password error')

語法二:

while 條件1:
     while 條件2:
         代碼1
         代碼2
         代碼3

示範一:

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        while True:
            print("""
            0 退出
            1 取款
            2 轉帳
            3 查詢
            """)
            choice=input('請輸入您要執行的操做:') #choice='1'
            if choice == '0':
                break
            elif choice == '1':
                print('取款。。。')
            elif choice == '2':
                print('轉帳。。。')
            elif choice == '3':
                print('查詢')
            else:
                print('輸入指令錯誤,請從新輸入')
        break
    else:
        print('username or password error')

示範二:

tag=True
while tag:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        while tag:
            print("""
            0 退出
            1 取款
            2 轉帳
            3 查詢
            """)
            choice=input('請輸入您要執行的操做:') #choice='1'
            if choice == '0':
                tag=False
            elif choice == '1':
                print('取款。。。')
            elif choice == '2':
                print('轉帳。。。')
            elif choice == '3':
                print('查詢')
            else:
                print('輸入指令錯誤,請從新輸入')
    else:
        print('username or password error')

結束while循環的兩種方式

    方式一:條件改成False

        在條件改成False時不會當即結束掉循環,而是要等到下一次循環判斷條件時纔會生效   

tag=True
    while tag:
        name=input('please input your name: ')
        pwd=input('please input your password: ')
    
        if name == 'egon' and pwd == '123':
            print('login successful')
            tag=False
        else:
            print('username or password error')
    
        print('===>')

    方式二:while+break / continue

        break必定要放在循環體內,一旦循環體執行到break就會當即結束本層循環

while True:
        name=input('please input your name: ')
        pwd=input('please input your password: ')
    
        if name == 'egon' and pwd == '123':
            print('login successful')
            break
        else:
            print('username or password error')
    
        print('===>>>>>')
        print('===>>>>>')

        while+continue:結束本次循環,直接進入下一次循環  

示例一:

count=1
while count < 6: #count=6
    if count == 4:
        count += 1
        continue
        
    print(count)
    count+=1

示例二:

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        break
    else:
        print('username or password error')
        # continue # 此處加continue無用

瞭解知識:

while + else:

    while 條件:
         代碼1
         代碼2
         代碼3
     else:
在循環結束後,而且在循環沒有被break打斷過的狀況下,纔會執行else的代碼

tag=True
while tag:
    print(1)
    print(2)
    print(3)
    # tag=False
    break
else:
    print('else的代碼')

3、for 循環

for循環的強大之處在於循環取值

l=['a','b','c','d','e']
#while 取數組代碼:

i=0
while i < len(l):
     print(l[i])
     i+=1

#for 取數組代碼:

for x in l: # x='b'
     print(x)

#for 取字典代碼:

dic={'name':'egon','age':18,'gender':'male'}

for x in dic:
     print(x,dic[x])

 

 

for + break

nums=[11,22,33,44,55]
for x in nums:
    if x == 44:
        break
    print(x)

for + continue

nums=[11,22,33,44,55]
for x in nums:
    if x == 22 or x == 44:
        continue
    print(x)

 

for + else

names=['egon','kevin1111_dsb','alex_dsb','mac_dsb']

for name in names:
    if name == 'kevin_dsb':
        break
    print(name)
else:
    print('======>')

for+ range()

# range的用法
>>> range(1,5)
[1, 2, 3, 4]
>>> for i in range(1,5):
...     print(i)
...
1
2
3
4
>>> range(1,5,1)
[1, 2, 3, 4]
>>> range(1,5,2) # 1 3
[1, 3]


for i in range(5): # 0 1 2 3 4
    print(i)
相關文章
相關標籤/搜索