Python:函数为2给出1个参数

时间:2014-02-24 22:08:52

标签: python class methods arguments

我在这个网站上看过类似的东西,并尝试使用以前的答案进行调试,但都失败了。

我正在测试(我没有写这个模块)一个模块,它将课程成绩的等级值从B-改为B,但从不跨越基础等级(即B +到A- )。

原始模块名为transcript.py 我正在我自己的testtranscript.py

中测试它

我正在通过导入它来测试该模块:'import transcript'和'import cornelltest' 我确保所有文件都在同一个文件夹/目录中。

transcript.py中存在函数raise_grade(此模块中有多个定义,但raise_grade是唯一一个给我带来麻烦的函数)。 ti的形式('class name','gradvalue')

已经有另一个定义将浮点数转换为字符串并返回(即3.0 - > B)。

def raise_grade(ti):
""""Raise gradeval of transcript line ti by a non-noticeable amount.


"""
# value of the base letter grade, e.g., 4 (or 4.0) for a 4.3
bval = int(ti.gradeval)
print 'bval is:"' + str(bval) + '"'

# part after decimal point in raised grade, e.g., 3 (or 3.0) for a 4.3
newdec = min(int((ti.gradeval + .3)*10) % 10, 3)
print 'newdec is:"' + str(newdec) + '"'

# get result by add the two values together, after shifting  newdec  one
# decimal place
newval = bval + round(newdec/10.0, 1)
ti.gradeval = newval
print 'newval is:"' + str(newval) + '"'

我可能会在以后删除打印件。

当我运行testtranscript时,它导入了transcript:

def test_raise():
"""test raise_grade"""
testobj = transcript.Titem('CS1110','B-')
transcript.raise_grade('CS1110','B-')
cornelltest.assert_floats_equal(3.0,transcript.lettergrade_to_val("B-"))

我是从cmd shell获得的: TypeError:raise_grade只需1个参数(给定2个)

Edit1:所以现在我看到,当raise_grade(ti)只是一个时,我给它提供了两个参数,但如果我把剩下的代码拿出来的话,它可能会有更多的亮点。我仍然坚持为什么我得到一个['str'对象没有等级错误]

LETTER_LIST = ['B', 'A']

# List of valid modifiers to base letter grades.
MODIFIER_LIST = ['-','+']

def lettergrade_to_val(lg):
"""Returns: numerical value of letter grade lg.

The usual numerical scheme is assumed: A+ -> 4.3, A -> 4.0, A- -> 3.7, etc.

Precondition: lg is a 1 or 2-character string consisting of a "base" letter
in LETTER_LIST optionally followed by a modifier in MODIFIER_LIST."""

# if LETTER_LIST or MODIFIER_LIST change, the implementation of
# this function must change.

# get value of base letter. Trick: index in LETTER_LIST is shifted from value
bv = LETTER_LIST.index(lg[0]) + 3
# Trick with indexing in MODIFIER_LIST to get the modifier value
return bv + ((MODIFIER_LIST.index(lg[1]) - .5)*.3/.5 if (len(lg) == 2) else 0)


class Titem(object):
"""A Titem is an 'item' on a transcript, like "CS1110 A+"

Instance variables:
    course [string]: course name.  Always at least 1 character long.

    gradeval [float]: the numerical equivalent of the letter grade.
                      Valid letter grades are 1 or 2 chars long, and consist
                      of a "base" letter in LETTER_LIST optionally followed
                      by a modifier in MODIFIER_LIST.
                      We store values instead of letter grades to facilitate
                      calculations of GPA later.

                      (In "real" life, one would write a function that,
                      when displaying a Titem, would display the letter
                      grade even though the underlying representation is
                      numerical, but we're keeping things simple for this
                      lab.)
"""

def __init__(self, n, lg):
    """Initializer: A new transcript line with course (name) n, gradeval
       the numerical equivalent of letter grade lg.

       Preconditions: n is a non-empty string.
       lg is a string consisting of a "base" letter in LETTER_LIST
       optionally followed by modifier in MODIFIER_LIST.
       """
    # assert statements that cause an error when preconditions are violated
    assert type(n) == str and type(lg) == str, 'argument type error'
    assert (len(n) >= 1 and  0 < len(lg) <= 2 and lg[0] in LETTER_LIST and
            (len(lg) == 1 or lg[1] in MODIFIER_LIST)), 'argument value error'

    self.course = n
    self.gradeval = lettergrade_to_val(lg)

Edit2:我理解原来的问题......但似乎原作者搞砸了代码,因为raise_grade对于3.7 --->的等级值不能正常工作。 4.0,因为bval采用原始的float并使其成为int,在这种情况下不起作用。

1 个答案:

答案 0 :(得分:1)

您正在调用该函数,您应该传递testobj

def test_raise():
    """test raise_grade"""
    testobj = transcript.Titem('CS1110','B-')
    transcript.raise_grade(testobj)
    ...

raise_grade函数期望单个参数ti具有gradeval属性,即Titem个实例。