2011-08-02 1 views
1

나는 파이썬을 조금 보게하기로 결정했다. 나는 this book가 그것을 읽기 시작하고 그것에서 약간 운동을했다는 것을 것을을 발견했다. 이제 저는 제 6 장에서 정확히 here입니다. 내 초보자 질문에 대해 유감스럽게 생각하지만이 test()는 어디서 오는가?test() - python에서 작동합니까? "컴퓨터 과학자처럼 생각하는 법"책

def mysum(xs): 
    """ Sum all the numbers in the list xs, and return the total. """ 
    running_total = 0 
    for x in xs: 
     running_total = running_total + x 
    return running_total 

#add tests like these to your test suite ... 
test(mysum([1, 2, 3, 4]), 10) 
test(mysum([1.25, 2.5, 1.75]), 5.5) 
test(mysum([1, -2, 3]), 2) 
test(mysum([ ]), 0) 
test(mysum(range(11)), 55) # Remember that 11 is not in the list that range generates. 

나는 그 책의 앞부분에서 언급하지 못했던 것 같습니다. test라는 모듈 만 발견했습니다. 이제 좀 혼란스러워, 내가 놓친 게 있니? 6 장에서이 함수를 사용하지 않는 Python 2.x 용이 책의 버전도 있습니다. 이 이상한 질문에 대해 초보자에게 계몽주의와 미안을 다시하십시오.

답변

2

연결된 챕터의 Section 6.7에 있습니다.

def test(actual, expected): 
    """ Compare the actual to the expected value, 
     and print a suitable message. 
    """ 
    import sys 
    linenum = sys._getframe(1).f_lineno # get the caller's line number. 
    if (expected == actual): 
     msg = "Test on line {0} passed.".format(linenum) 
    else: 
     msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'." 
            . format(linenum, expected, actual)) 
    print(msg) 
+0

확인을 연습합니다. 이 글은 내가 읽었던 버전에는 존재하지 않는다. 비교 : [일반] (http://openbookproject.net/thinkcs/python/english3e/index.html) 및 [RLE Edition] (http://openbookproject.net/thinkcs/python/english3e-rle/index.html) 아하. – pythonn00b

1

제 12 장 [사전]에서 같은 문제가 발생합니다. 다른 해결 방법이 있습니다.

def test(expression1, expression2): 
    if expression1 == expression2: 
     return 'Pass' 
    else: 
     return 'Fail' 

이 모든 당신이 나와있는 표현뿐만 아니라 제 12 장 [사전]에 나와있는 것들에 대한 작업을 특별히 2.

관련 문제