DAY 6
0

## Day6 Python 基礎 - for流程判斷

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

for i in range(10):
print("loop:", i)

---------------執行結果---------------

loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

for i in range(0, 10, 2):
print("loop:", i)

---------------執行結果---------------

loop: 0
loop: 2
loop: 4
loop: 6
loop: 8

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

age_of_ironman = 35

for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
else:
print("You have tried too many, exit")

---------------執行結果---------------

# 故意猜超過三次，看看結果
guess age:1
You may think bigger...
guess age:2
You may think bigger...
guess age:3
You may think bigger...
You have tried too many, exit

Process finished with exit code 0

# 先試試在三次內打對密碼，看看結果
guess age:4
You may think bigger...
guess age:35
Bingo, You got it!!!

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

age_of_ironman = 35

count = 0

while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1
if count == 3:
countine_confirm = input("Do you want to keep go in ? (y/n)")
if countine_confirm != 'n':
count = 0
else:
print("You have tried too many, exit")

---------------執行結果---------------

# 故意猜錯三次，第二次詢問時，選n
guess age:1
You may think bigger...
guess age:2
You may think bigger...
guess age:3
You may think bigger...
Do you want to keep go in ? (y/n)
guess age:4
You may think bigger...
guess age:5
You may think bigger...
guess age:6
You may think bigger...
Do you want to keep go in ? (y/n)n
You have tried too many, exit

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

age_of_ironman = 35

count = 0

while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1
if count == 3:
countine_confirm = input("Do you want to keep go in ? (y/n)")
if countine_confirm != 'n':
count = 0

---------------執行結果---------------

# 故意猜錯三次，第二次詢問時，選n
guess age:1
You may think bigger...
guess age:2
You may think bigger...
guess age:3
You may think bigger...
Do you want to keep go in ? (y/n)y
guess age:4
You may think bigger...
guess age:5
You may think bigger...
guess age:6
You may think bigger...
Do you want to keep go in ? (y/n)n

Process finished with exit code 0
``````

• 語法：`range(start, stop[, step])` 裡面的 `step 預設是 1`

Python 學習筆記29