gpt4 book ai didi

python - python 中的 test() 函数?预订 "How to Think Like a Computer Scientist"

转载 作者:行者123 更新时间:2023-11-30 23:51:16 25 4
gpt4 key购买 nike

我决定深入研究一下Python。我发现this book开始阅读它并做一些练习。现在我被困在第 6 章,正是 here 。抱歉我的新手问题,但是这个 test() 函数从哪里来?

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 的模块。现在我有点困惑,我错过了什么吗?本书还有一个针对 Python 2.x 的版本,在第 6 章中没有使用此函数......请赐教新手,对于这个奇怪的问题再次表示歉意。

最佳答案

位于 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)

关于python - python 中的 test() 函数?预订 "How to Think Like a Computer Scientist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com