0225 函式 def 自定義函式的格式

2021-09-11 08:02:42 字數 1179 閱讀 8009

函式介紹:

def guide():

print('-2f 停車場')

print('-1f 超市')

print(' 1f 珠寶')

print(' 2f 服裝')

print(' 3f 美食')

print(' 4f 電影院')

floor=int(input('請輸入您所在的樓層:'))

if floor==1:

print('歡迎來到大悅城一樓!')

guide()

elif floor==2:

print('歡迎來到大悅城二樓!')

guide()

elif floor==3:

print('歡迎來到大悅城三樓!')

guide()

elif floor==4:

print('歡迎來到大悅城四樓!')

guide()

elif floor==-1:

def 自定義函式的格式:
#定義函式:

def 函式名([形參]):

函式體[返回值]

#呼叫函式:

函式名([實參])

個人見解:

將函式看做乙個黑箱,形參就是黑箱的介面,實參就是輸入,返回值就是黑箱的輸出。

def guide():

print('-2f 停車場')

print('-1f 超市')

print(' 1f 珠寶')

print(' 2f 服裝')

print(' 3f 美食')

print(' 4f 電影院')

floor=int(input('請輸入您所在的樓層:'))

if floor==1:

print('歡迎來到大悅城一樓!')

guide()

elif floor==2:

print('歡迎來到大悅城二樓!')

guide()

elif floor==3:

print('歡迎來到大悅城三樓!')

guide()

elif floor==4:

print('歡迎來到大悅城四樓!')

guide()

elif floor==-1:

Python自定義函式def

python自定義函式以def開頭,空一格之後是這個自定義函式的名稱,名稱後面是一對括號,括號裡放置形參列表,結束括號後面一定要有冒號,函式的執行體程式 也要有適當的縮排。1 標準自定義函式 形參列表是標準的tuple資料型別。def quadratic a,b n a 2 b return n q...

自定義函式(def) 函式與過程

1.函式返回值 def say print itxds print say itxds none 函式沒有顯性返回時,預設返回none 2.變數作用域 區域性變數 def cal amount,rate finalamount amount rate return finalamount cal 1...

Python初步學習 def自定義函式的簡單使用

你可以定義乙個由自己想要功能的函式,以下是簡單的規則 任何傳入引數和自變數必須放在圓括號中間,圓括號之間可以用於定義引數。函式的第一行語句可以選擇性地使用文件字串 用於存放函式說明。函式內容以冒號起始,並且縮排。return 表示式 結束函式,選擇性地返回乙個值給呼叫方。不帶表示式的return相當...