Module 2
The HtDF recipe consists of the following steps:
Step 1: Write the stub, including signature and purpose
Step 2: Define examples
Step 3: Write the template
Step 4: Code the function body
Step 5: Test and debug until correct
Step 1: Write the stub, including signature and purpose
@typecheck
def double(n: float) -> float: #signature
"""
returns n doubled
"""#purpose(属于stub)
return 0 #stub[取决于output类型]
Stub作用:The stub serves as a scaffolding to make it possible to run the examples even before the function design is complete.
Step 2: Define examples
from cs103 import *
@typecheck
def double(n: float) -> float:
"""
returns n doubled
"""
return 0 #stub
________________________________下面的是examples
# Begin testing
start_testing()
expect(double(0), 2 * 0)
expect(double(-2.1), 2 * -2.1)
expect(double(10.11), 2 * 10.11)
# Show testing summary
summary()
Tips:
1.Examples 不光是 HtDD中的L10=12.3 这种的examples,在HtDF中就是这种在testing中的Examples
同时具有test 和eaxmples的作用
2.每个情况都要有一个 examples,不要疏漏
Step 3: Write the template
包含所有的自变量,ex:例子中的 n
from cs103 import *
@typecheck
def double(n: float) -> float:
"""
returns n doubled
"""
# return 0 #stub
________________________________下面的是template
return ...(n) #template
________________________________下面的是examples
# Begin testing
start_testing()
expect(double(0), 0)
expect(double(-2.1), 2 * -2.1)
expect(double(10.11), 2 * 10.11)
# Show testing summary
summary()
Step 4: Code the function body
@typecheck
def double(n: float) -> float:
"""
returns n doubled
"""
# return 0 #stub
# return ...(n) #template
return 2*n
给template前加“#”
同时给方程body(灵魂运算)