Python 入門指南 V2.00 - 單元 7 範例及練習程式碼



while_demo.py


print()
i = 10 # 設定控制變數
while i > 0:
   # 迴圈工作區
   print(i)
   i -= 1 # 調整控制變數值
print()
 
# 檔名: while_demo.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

for_demo.py


print()
for i in range(10, 0, -1):
   print(i)
print()
 
# 檔名: for_demo.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0701.py


sum = 0
i = 1
while i < 1001:
   sum += i
   i += 1
print(sum)
 
# 檔名: exercise0701.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0702.py


sum = 0
end = int(input("end: "))
i = 1
while i < end + 1:
   sum += i
   i += 1
print(sum)
 
# 檔名: exercise0702.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0703.py


i = 0
j = 0
while i < 5:
   while j <= i:
      print("*", end="")
      j += 1

   print()
   j = 0
   i += 1
 
# 檔名: exercise0703.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0704.py


i = 0
j = 5
while i < 5:
   while j > i:
      print("*", end="")
      j -= 1

   print()
   j = 5
   i += 1
 
# 檔名: exercise0704.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0705.py


i = 0
j = int(input("len: "))
k = j
while i < j:
   while j > i:
      print("*", end="")
      j -= 1

   print()
   j = k
   i += 1
 
# 檔名: exercise0705.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0706.py


i = 1
j = 1
while i < 10:
   while j < 10:
      print(i * j, end=", ")
      j += 1

   print()
   j = 1
   i += 1
 
# 檔名: exercise0706.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0707.py


while True:
   a = input(": ")
   if a == "e":
      break
 
# 檔名: exercise0707.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0708.py


i = 1
p = 1
while i <= 10:
   p *= i
   i += 1 
print(p)
 
# 檔名: exercise0708.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0709.py


end = int(input("p: "))
i = 1
p = 1
while i <= end:
   p *= i
   i += 1 
print(p)
 
# 檔名: exercise0709.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0710.py


n1 = 0
n2 = 1
i = 3
while i <= 20:
   n3 = n1 + n2
   n1 = n2
   n2 = n3
   i += 1
print(n3)
 
# 檔名: exercise0710.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0711.py


n1 = 0
n2 = 1
i = 3
end = int(input("f: "))
while i <= end:
   n3 = n1 + n2
   n1 = n2
   n2 = n3
   i += 1
print(n3)
 
# 檔名: exercise0711.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

the end

2 則留言:

匿名 提到...

你好
請問可以請教問題嗎? 我找不到適當的地方發問 若有不當之處請刪除留言
關於 exercise 0705 ,
1. 為何要定義K值?
2. i 的定義是?寬?
謝謝

Kaiching Chang 提到...

k 是為了保存 j 的值,其他請參考題目。