[Python]Function Design Receipe

The Six Steps

  1. Examples
    • What should your function do?
    • Type a couple of example calls.
    • Pick a name (often a verb or verb phrase): What is a short answer to "What does your function do"?
  2. Type Contract
    • What are the parameter types?
    • What type of value is returned?
  3. Header
    • Pick meaningful parameter names.
  4. Description
    • Mention every parameter in your description.
    • Describe the return value.
  5. Body
    • Write the body of your function.
  6. Test
    1. Run the examples.

Applying the Design Recipe

The problem:

The United States measures temperature in Fahrenheit and Canada measures it in Celsius. When travelling between the two countries it helps to have a conversion function. Write a function that converts from Fahrenheit to Celsius.

  1. Examples
        >>> convert_to_ccelsius(32)
        0
        >>> convert_to_celsius(212)
        100
        
    
  2. Type Contract
        (number) -> number
        
    
  3. Header
        def convert_to_celsius(fahrenheit):
        
    
  4. Description
        Return the number of Celsius degrees equivalent to fahrenheit degrees.
        
    
  5. Body
            return (fahrenheit - 32) * 5 / 9
        
    
  6. Test
         Run the examples.   
        
    
Putting it all together:
def convert_to_celsius(fahrenheit):
   ''' (number) -> number
   
   Return the number of Celsius degrees equivalent to fahrenheit degrees.
   
   >>> convert_to_ccelsius(32)
   0
   >>> convert_to_celsius(212)
   100
   '''
   
   return (fahrenheit - 32) * 5 / 9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值