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



big_demo.py


def big(a, b):
   if a > b:
      return a
   else:
      return b
  
print()
print(big(33, 22))
print(big("John", "Mary"))
print()
 
# 檔名: big_demo.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

print_demo.py


def print_newline():
   print()
  
def print_something(a):
   print(a)
  
def return_something(a):
   return a
  
print_newline()
print_something(return_something("There is no spoon."))
print_newline()
 
# 檔名: print_demo.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0801.py


def sum(a, b):
   return a + b

print(sum(11, 22))
print(sum(22, 33))
print(sum(33, 44))
 
# 檔名: exercise0801.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0802.py


def sum(a, b):
   return a + b

n1 = int(input("n1: "))
n2 = int(input("n2: "))
print(sum(n1, n2))
 
# 檔名: exercise0802.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0803.py


def sum(p):
   i = 1
   s = 0
   while i <= p:
      s += i
      i += 1
   
   return s

print(sum(10))
print(sum(11))
print(sum(12))
 
# 檔名: exercise0803.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0804.py


def sum(p):
   i = 1
   s = 0
   while i <= p:
      s += i
      i += 1
   
   return s

end = int(input("n: "))
print(sum(end))
 
# 檔名: exercise0804.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0805.py


def factorial(n):
   i = 1
   p = 1
   while i <= n:
      p *= i
      i += 1 
   
   return p

print(factorial(10))
print(factorial(11))
print(factorial(12))
 
# 檔名: exercise0805.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0806.py


def factorial(n):
   i = 1
   p = 1
   while i <= n:
      p *= i
      i += 1 
   
   return p

n = int(input("n: "))
print(factorial(n))
 
# 檔名: exercise0806.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0807.py


def fibonacci(n):
   n1 = 0
   n2 = 1
   i = 3
   while i <= n:
      n3 = n1 + n2
      n1 = n2
      n2 = n3
      i += 1

   return n3

print(fibonacci(10))
print(fibonacci(11))
print(fibonacci(12))
  
# 檔名: exercise0807.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0808.py


def fibonacci(n):
   n1 = 0
   n2 = 1
   i = 3
   while i <= n:
      n3 = n1 + n2
      n1 = n2
      n2 = n3
      i += 1

   return n3

n = int(input("n: "))
print(fibonacci(n))
  
# 檔名: exercise0808.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

the end

沒有留言: