if判斷、while循環、for循環

1、if 判斷

語法一:if判斷ide

sex = 'female'
age = 32
is_beautiful = True
if sex == 'female' and age == 18 and is_beautiful:
    print('Begin your love words:')
print('another code1')
print('another code2')

if判斷使用注意事項:if條件緊跟着一個冒號:+回車,pycharm會自動進入到下一行並縮進4個空格,開始進入運行一組代碼塊。在這裏注意中英文輸入法對代碼「括號、冒號、逗號」這些字符的影響,謹記必定要用英文輸入法輸入,否則容易出現報錯。spa

 

語法二:if+else判斷code

sex = 'female'
age = 18
is_beautiful = True
if sex == 'female' and 16 < age <24 and is_beautiful:
    print('Begin your love words.')
else:
    print('Say nothing...')

語法三:if+else 的嵌套blog

sex = 'female'
age = 18
is_beautiful = True
is_success = True
if sex == 'female' and 16 < age <24 and is_beautiful:
    print('Begin your love words.')
    if is_success:
        print('HaHaHa')
    else:
        print('I hate love,go away!')
else:
    print('Say nothing...')

tip:①and 多個條件並過長時,可用‘\+回車’來多行顯示,這裏不影響語法。ip

       ②快捷操做,TAB鍵 能夠進行快捷縮進,好比選中多行一塊兒快捷縮進,要回退的話,按shift+tab組合鍵。pycharm

語法四:if+elif+elseinput

# 輸入成績判斷優秀、很好、通常、不好
score = input('Please input your score:')
score = int(score)
if score >= 90:
    print('優秀')
elif score >= 80:
    print('很好')
elif score >= 70:
    print('通常')
else:
    print('不好')

小練習:io

name = input('請輸入您的用戶名:')
pwd = input('請輸入您的密碼:')
if name == 'sgt' and pwd == '123':
    print('登錄成功')
else:
    print('用戶名或密碼錯誤')
練習一:用戶登錄驗證
sgt = 'spueradministrator'
sgf = 'systemadministrator'
shr = 'administrator'
sqs = 'guest'
name = input('please input your username:')
if name == 'sgt':
    print('你好,超級管理員!')
elif name =='sgf':
    print('你好,系統管理員!')
elif name == 'shr':
    print('你好,管理員!')
elif name == 'sqs':
    print('你好,賓客!')
根據用戶輸入內容打印其權限
while True:
    today = input('今天是星期幾?')
    if today == 'monday' or today == 'tuesday' or today == 'wednesday'\
        or today == 'thursday' or  today == 'friday':
        print('上班')
        break
    elif today == 'saturday' or today == 'sunday':
        print('出去浪')
        break
    else:
        print('''
        必須輸入如下任意一種:
        monday
        tuesday
        wednesday
        thursday
        friday
        saturday
        sunday
        ''')
上班或出去浪

 

2、while循環

方式一:while+True/Falsefor循環

tag = True
while tag:
    name = input('please input your name:')
    pwd = input('please input your password:')
    if name == 'sgt' and pwd == '123':
        print('congratulation,region success!')
        tag = False
    else:
        print('your name or password is wrong,please try again!')

 

方法二:while+breakevent

while True:
    name = input('please input your name:')
    pwd = input('please input your password:')
    if name == 'sgt' and pwd == '123':
        print('congratulation,region success!')
        break
    else:
        print('your name or password is wrong,please try again!')

break是終止本層while循環。

方法三:while+continue

nums = 1
while nums <= 6:
    if nums == 4 or nums == 5:
        nums += 1
        continue
    else:
        print(nums)
        nums += 1

終止當前循環,直接進入下次循環。

方法四:while+else

nums = 1
while nums <= 6:
    if nums == 4 or nums == 5:
        nums += 1
        continue
    else:
        print(nums)
        nums += 1
else:
    print('前面代碼無break,else 條件成立,不然else不執行。')

方法五:while的嵌套

示例一:

while True:
    name = input('please input your username:')
    pwd = input('please input your password:')
    if name == 'egon' and pwd == '123':
        print('login successful')
        while True:
            print('''
            0_退出
            1_取款
            2_轉帳
            3_查詢                   
            ''')
            choice = input('請輸入您要執行的操做:')
            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 username:')
    pwd = input('please input your password:')
    if name == 'egon' and pwd == '123':
        print('login successful')
        while tag:
            print('''
            0_退出
            1_取款
            2_轉帳
            3_查詢       
            ''')
            choice = input('請輸入您要執行的代碼:')
            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')

3、for 循環 (其強大之處在於循環取值)

方案1、

for循環取值列表:

取值若是用while循環:
l = ['a', 'b', 'c', 'd', 'e']
i = 0
while i < len(l):                   #這裏的len(l)表示計算列表l的長度(也就是索 
                                    #引數) 
    print(l[i])                      
    i += 1                         


若是咱們使用for循環取值:
l = ['a', 'b', 'c', 'd', 'e']
for x in l:                        #這裏的x in l 是直接將l列表裏的數據取出關聯 
     print(x)                      #到x,不用額外賦值。

for循環取值字典:

info = {'sgt': 'me', 'sgf': 'brother', 'age': 18, 'sun': 'shr'}
for x in info:
    print(x, info[x])

方案2、for+break

取出列表前幾個數,後面的不取

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

方案3、for+continue

不取其中的多個數據

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

方案4、for+else

names = ['egon', 'alexdsb', 'sb', 'dsb']
for name in names:
    if name == 'alexdsb':
        break
    print(name)
else:
    print('>>>>>>>>>>>')

#有break ,else下面的print不執行。

names = ['egon', 'alexdsb', 'sb', 'dsb']
for name in names:
    if name == 'alexdsb':
        continue
    print(name)
else:
    print('>>>>>>>>>>>')
#若是無break,else下面的print執行。

方案5、for+range()

range的用法:

>>> range(5)
[0, 1, 2, 3, 4]
>>> range(1,5)
[1, 2, 3, 4]
>>> range(1,5,2)
[1, 3]
>>> range(1,1)
[]
>>>
正規格式爲range(1,5,2)
表示:從1開始計數,每計數一次遞增2,因此結果是[1,3]
若是括號裏只有一個數,表明(0,5,1),即從0開始計數,遞增數爲1.

示例:

for x in range(5):
    print(x)

#結果:
#0
#1
#2
#3
#4

方案6、for循環嵌套

for x in range(3):
    for y in range(3):
        print(x, y)
'''
結果
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
'''
相關文章
相關標籤/搜索