-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteachCode_002.py
More file actions
137 lines (102 loc) · 3.38 KB
/
teachCode_002.py
File metadata and controls
137 lines (102 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# _*_ coding: utf-8 _*_
# 迴圈(while & for) 練習
# By Eosin Chen([email protected])
import random
# while 基本型
def while_basic():
random_count = random.randint(1, 6)
print(random_count)
# while random_count != 6: <= 判斷式上,加上括號比較好
while (random_count != 6):
random_count = random.randint(1, 6)
print(random_count)
# 依照輸入的值來執行 while 次數
def while_input():
while_count = 0 #用來記錄迴圈執行次數
# 這兒有資料型態轉換 => int(...)
input_count = int(input("請輸入迴圈執行次數:"))
print(input_count)
while (while_count < input_count):
while_count += 1 # while_count = while_count + 1
print("目前執行到第 " + str(while_count) + " 次")
# ------------------------------------------------------------------------------
# for 迴圈 - 以 range 為範圍來執行
def for_range():
# range(start, stop, step) <= start 到 stop-1, 以 step 為遞增值(若無內定為 1)
for var in range(1, 6):
print(var)
# for 迴圈 - 從資料結構中(變數),取出值來執行
def for_string():
myEngString = "My First String"
myChnString = "我的中文字串"
for char in myChnString:
print(char)
# for 迴圈 - 從資料結構中(串列),取出值來執行
def for_list():
myList = [1, 3, 5, 7, 9, "A", "String"]
for char in myList:
print(char)
# for 迴圈 - 巢狀,九九乘法表
def for_nested():
for i in range(2, 7, 4):
for j in range(1, 10):
# -----
print("{}x{}={:>2} ".format(i, j, i * j), end="")
print("{}x{}={:>2} ".format(i + 1, j, (i + 1) * j), end="")
print("{}x{}={:>2} ".format(i + 2, j, (i + 2) * j), end="")
print("{}x{}={:>2} ".format(i + 3, j, (i + 3) * j))
#-----
#-----
#for k in range(i, i + 5):
# print("{}x{}={:>2} ".format(k, j, k * j), end="")
#print()
#-----
print()
# continue - for 迴圈 - 巢狀,九九乘法表
def for_nested_continue():
for i in range(2, 9):
if i != 2 and i != 6: continue
for j in range(1, 10):
for k in range(i, i + 5):
print("{}x{}={:>2} ".format(k, j, k * j), end="")
print()
print()
# break - while - random
def while_basic_break():
while True:
x = random.randint(1, 6)
print(x)
if x == 6: break
# 費氏函數 - loop 版
def fib_loop(n):
# 用來存放最終值
sum_final = 1
# 用來存放前一個的值
sum_prev_1 = 0
# 數值移動時,暫存之用
sum_temp = 0
# 進行 n < 0, n == 0, n == 1 的處理
if (n < 0):
print("請輸入 0 以上的整數")
return
if (n == 0) or (n == 1):
return n
# 利用迴圈進行數值運算
for i in range(2, n + 1):
sum_temp = sum_final
sum_final = sum_final + sum_prev_1
sum_prev_1 = sum_temp
# 顯示每一個階段的結果,以便除錯
# 正式程式中,此行需移除
print(str(i) + " => " +str(sum_final) + ", " + str(sum_prev_1) + ", " + str(sum_temp))
return sum_final
# 主程式
#while_basic()
#while_input()
#for_range()
#for_string()
#for_list()
#for_nested()
#for_nested_continue()
#while_basic_break()
fib_loop(100)