while循環與for循環

while循環

while 條件 :
代碼塊python

inp_age = input('>>>Please input the age :')
inp_age = int(inp_age)

if inp_age < 18:
    print("it's too small!")
elif inp_age == 18:
    print('congratulatin!!!')
else:
    print("it's too big!!!")
>>>Please input the age :23
it's too big!!!
while True:
    inp_age = input('>>>Please input the age :')
    inp_age = int(inp_age)
    if inp_age > 18:
        print("it's too big!!!")
    elif inp_age == 18:
        print('Congratulations!!!')
        break
    else:
        print("it's too samll!!!")
>>>Please input the age :16
it's too samll!!!
>>>Please input the age :19
it's too big!!!
>>>Please input the age :18
Congratulations!!!
# 三條命
life = 0
count = 3
while life < count:
    inp_age = input('>>>Please input the age :')
    inp_age = int(inp_age)
    if inp_age < 18:
        print("it's too small!!!")
    elif inp_age == 18:
        print('Congratulation!!!')
        break
    else:
        print("it's too big!!!")
    life += 1
>>>Please input the age :16
it's too small!!!
>>>Please input the age :19
it's too big!!!
>>>Please input the age :20
it's too big!!!
# if  , if ...else,if... elif....else ...的嵌套
# 一次猜對得金牌,二次猜對得銀牌,三次得銅牌

life = 0
count = 3
while life < count:
    inp_age = input('>>>Please input the age :')
    inp_age = int(inp_age)
    if inp_age < 18:
        print("it's too small!")
    elif inp_age == 18:
        if life == 0:
            print('Gold medal!!!')
        elif life == 1:
            print('Silver medal!!!')
        else:
            print('Bronze medal!!!')
        break
    else:
        print("it's too big!")
    life += 1
>>>Please input the age :17
it's too small!
>>>Please input the age :18
Silver medal!!!
# 獎品能夠選擇
life = 0
count = 3
award_dict = {0: 'an apple', 1: 'an orange', 2: 'a banana'}
while life < count:
    inp_age = input('>>>Please input the age :')
    inp_age = int(inp_age)
    if inp_age > 18:
        print("it's too big!")
    elif inp_age == 18:
        print(f'Congratulations!!! \nPlease choose your award:{award_dict}')
        choice = input('>>>Please input the number :')
        choice = int(choice)
        get_award = award_dict[choice]
        print(f'Your award is {get_award} ')
        break
    else:
        print("it's too small!")
    life += 1
>>>Please input the age :19
it's too big!
>>>Please input the age :18
Congratulations!!! 
Please choose your award:{0: 'an apple', 1: 'an orange', 2: 'a banana'}
>>>Please input the number :1
Your award is an orange
# 選擇多個獎品
life = 0
count_of_game = 3
count_of_award = 0
award_dict = {0: 'an apple', 1: 'an orange', 2: 'a banana'}
while life < count_of_game:
    inp_age = input('>>>Please input the age :')
    inp_age = int(inp_age)
    if inp_age < 18:
        print("it's too small!")
    elif inp_age == 18:
        print(f'Congratulations!!! \nPlease choose your award: {award_dict}')
        while count_of_award < 3:
            choice = input('>>>Please input the number :')
            choice = int(choice)
            get_award = award_dict[choice]
            print(f'Your award is {get_award}!')
            count_of_award += 1
        break
    else:
        print("it's too big!")
    life += 1
>>>Please input the age :20
it's too big!
>>>Please input the age :18
Congratulations!!! 
Please choose your award: {0: 'an apple', 1: 'an orange', 2: 'a banana'}
>>>Please input the number :1
Your award is an orange!
>>>Please input the number :0
Your award is an apple!
>>>Please input the number :2
Your award is a banana!

while + continue(掌握)

# 計算0+1+2....+99+100

i = 0
sum = 0
while i < 100:
    i += 1
    sum += i
print(sum)
5050
# continue 不執行下面的代碼,跳出本次循環,進行下次循環。

i = 0
sum = 0
while i < 100:
    i += 1
    if i == 20 or i == 30:
        continue
    sum += i
print(sum)
5000

continue 與 break

break直接結束while循環,若是循環100次,在50次時使用break,後面50次不在循環,結束本層循環
continue若是循環100次,在50次時使用continue,第50次不循環,第51次繼續循環,結束本次循環。app

while + else 之後儘可能不要使用

若是while循環結束,且沒有被break,則執行else內代碼。code

i = 0
while i < 5:
    i += 1
    if i == 4:
        break
else:
    print("I'm not breaking!")
print(i)
4
i = 0
while i < 5:
    i += 1
    if i == 6:
        break
else:
    print("I'm not breaking!")
print(i)
I'm not breaking!
5

for循環

while循環可控,可是控制很差容易死循環。get

my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']

print(my_info_list[0])
print(my_info_list[1])
print(my_info_list[2])
print(my_info_list[3])
name
height
weight
['running', 'reading']
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
i = 0
while i < len(my_info_list):
    print(my_info_list[i])
    i += 1
name
height
weight
['running', 'reading']
address
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']

for i in my_info_list:
    print(i)
name
height
weight
['running', 'reading']
address
# for + break
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']

for i in my_info_list:
    if i == 'weight':
        break
    print(i)
name
height
# for + continue
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']

for i in my_info_list:
    if i == 'weight':
        continue
    print(i)
name
height
['running', 'reading']
address
# for ...else...若是for循環沒有被break,則執行else代碼。
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']


for i in my_info_list:
    if i == 10:
        break
    print(i)
else:
    print("I'm not breaking!")
name
height
weight
['running', 'reading']
address
I'm not breaking!

while 和 for 循環的異同

相同點:都是循環
不一樣點:while須要條件,for循環不須要input

for循環實現loading

import time
print('loading', end='')
for i in range(6):
    print('.', end='')
    time.sleep(0.3)
loading......

登錄

username = 'William'
password = '123'
count = 0
choice = 0
function_dict = {0: 'reading', 1: 'watching', 2: 'chatting', 3: 'logout'}
while count < 3:
    inp_username = input('>>>Please input your name:')
    inp_password = input('>>>Please input your password:')
    if inp_username == username and inp_password == password:
        re_password = input('>>>Please input your password again:')
        if re_password == inp_password:
            print(
                f'login successfully! \nPlease choose the function:{function_dict}')
            while True:
                inp_function = int(input('>>>Please choose:'))
                function = function_dict[inp_function]
                if inp_function == 3:
                    print('You are logout!')
                    count = 5
                    break
                else:
                    print(f'You are {function}')
        else:
            print('Passwords are different!')
    else:
        print('Username or password is incorrect!')
    count += 1
>>>Please input your name:William
>>>Please input your password:123
>>>Please input your password again:123
login successfully! 
Please choose the function:{0: 'reading', 1: 'watching', 2: 'chatting', 3: 'logout'}
>>>Please choose:0
You are reading
>>>Please choose:1
You are watching
>>>Please choose:1
You are watching
>>>Please choose:2
You are chatting
>>>Please choose:3
You are logout!
相關文章
相關標籤/搜索