自訂函式語法
身為 IT 支援團隊的一員,你常需要把員工姓名轉換成公司的電子郵件地址。公司採用標準格式:名(小寫)+ 點 + 姓(小寫)+ @techcompany.com。例如,「Jane Doe」會變成 [email protected]。
與其每次手動建立這些信箱,你將撰寫一個自訂函式來自動化這項工作。
本練習屬於課程
Intermediate Python for Developers
練習說明
- 定義一個名為
generate_email的函式,接收一個名為full_name的引數。 - 在函式中回傳產生的
email。 - 對字串
full_name呼叫這個函式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
full_name = "Alan Turing"
# Define the generate_email function
____ ____(full_name):
name_parts = full_name.split()
email = name_parts[0].lower() + '.' + name_parts[1].lower() + '@techcompany.com'
# Return the email address
____ ____
# Call the function on the full_name string
print(____(____))