首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:函数接受2个给定参数的一个参数

Python:函数接受2个给定参数的一个参数
EN

Stack Overflow用户
提问于 2014-02-24 22:08:52
回答 1查看 1.5K关注 0票数 0

我已经在这个网站上寻找类似的东西,并试图使用以前的答案进行调试,但失败了。

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

最初的模块名为transcript.py,我正在自己的testtranscript.py中测试它

我正在通过导入模块来测试这个模块:‘importing’和‘importing’--我确保了所有文件都在同一个文件夹/目录中。

raise_grade函数存在于transcript.py中(在这个模块中有多个定义,但raise_grade是唯一给我带来麻烦的)。ti为形式(“类名”、“梯度值”)

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

代码语言:javascript
复制
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) + '"'

我以后可能会把指纹处理掉。

当我运行transcript时,它将导入文本:

代码语言:javascript
复制
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只接受一个参数(2给定)

Edit1:所以现在我看到,当raise_grade(ti)只是一个参数时,我给了它两个参数,但是如果我只输出剩下的代码,它可能会发出更多的信息。我仍然被困在为什么得到'str‘对象没有分级错误

代码语言:javascript
复制
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采用了原始的浮点数,并使它成为一个int,在本例中不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-24 22:31:00

如果调用函数不正确,则应该传递testobj

代码语言:javascript
复制
def test_raise():
    """test raise_grade"""
    testobj = transcript.Titem('CS1110','B-')
    transcript.raise_grade(testobj)
    ...

raise_grade函数需要一个参数ti,该参数具有gradeval属性,即Titem实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22000136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档