找回密碼
 立即註冊
查看: 1750|回復: 4

define function

[複製鏈接]

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

發表於 2016-9-20 18:23:51 | 顯示全部樓層 |閱讀模式
  1. def spam():
  2.     """ define function """
  3.     print "Eggs!"

  4. spam()
複製代碼


938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-9-21 17:26:14 | 顯示全部樓層
  1. def shut_down(s):
  2.     if s == "yes":
  3.         return "Shutting down"
  4.     elif s == "no":
  5.         return "Shutdown aborted"
  6.     else:
  7.         return "Sorry"
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-9-23 16:21:15 | 顯示全部樓層
  1. def hotel_cost(nights):
  2.     return 140 * nights

  3. def plane_ride_cost(city):
  4.     if city == "Charlotte":
  5.         return 183
  6.     elif city == "Tampa":
  7.         return 220
  8.     elif city == "Pittsburgh":
  9.         return 222
  10.     elif city =="Los Angeles":
  11.         return 475
  12.     else:
  13.         return 0
  14.    
  15. def rental_car_cost(days):
  16.     total = 40 * days
  17.     if days >= 7:
  18.         total -= 50
  19.     elif days >= 3 and days<7:
  20.         total -= 20
  21.     return total

  22. def trip_cost(city, days, spending_money):
  23.     return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

  24. print trip_cost("Los Angeles", 5, 600)
複製代碼

計算旅遊花費

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2017-3-6 13:56:46 | 顯示全部樓層
  1. def count_small(numbers):
  2.     total = 0
  3.     for n in numbers:
  4.         if n < 10:
  5.             total = total + 1
  6.     return total

  7. lost = [4, 8, 15, 16, 23, 42]
  8. small = count_small(lost)
  9. print small
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2017-3-6 14:04:54 | 顯示全部樓層
  1. def fizz_count(x):
  2.     count = 0
  3.     for item in x:
  4.         if item == "fizz":
  5.             count = count + 1
  6.     return count

  7. fizz_count(["fizz","cat","fizz"])
複製代碼
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DD論壇 維護: Redd Design

GMT+8, 2024-9-20 00:22 , Processed in 0.037714 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表