# __author__:Nzkalhbxx
# __date__:17-10-29
import time
# 函數的定義:
"""
def functionName(形參1, 形參2...):
函數體...
"""
def eatApple():
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
print("you should eat %s %s at %s"%("apple", 3, time_current))
def eatBanana():
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
print("you should eat %s %s at %s"%("Banana", 7, time_current))
def eat_passion_fruit():
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
print("you should eat %s %s at %s"%("passion_fruit", 11, time_current))
eatApple()
eatBanana()
eat_passion_fruit()
print("使用參數".center(33, "-"))
def eat(fruit, num, name=""):
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
if name != "":
print(name, end=", ")
print("you should eat %s %d at %s" % (fruit, num, time_current))
# 只有當形參有默認值時可以缺省, 否則eat() missing 1 required positional argument: 'name'
eat("apple", 7)
# 當一個實參使用key = value傳遞參數時順序可以隨意, 但未使用k-v的參數必須寫在前面
eat("banana", name="", num=3)
# 當不使用key = value時, 參數順序必須一一對應
eat("passion_fruit", 7, "psj")
# eat("passion_fruit", "psj", 19) # TypeError: %d format: a number is required, not str
運行結果