attributeError(“''u AssertRaisesContext'对象没有属性'exception'”),

2024-04-20 09:55:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程的初学者。我真的不明白这个错误指的是什么。任何帮助将不胜感激。 该法应按如下方式计算某个国家人民的税收: 年收入:0-1000

税率:0%

年收入:1001-10000

税率:10%

年收入:10001-20200

税率:15%

年收入:20201-30750

税率:20%

年收入:30751-50000

税率:25%

年收入:5万以上

税率:30%

我的代码:

def calculate_tax(pDict):
  if type(pDict) is dict:
    try:
      length = len(pDict)
      count = 0
      #decleration of new updated dictionary
      dict_list = {}
      while count<length:
        #calculate yearly tax
        #countdown of values in the dictionary until the last value
        totals = list(pDict.values())[count]
        totals = int(totals)
        names = list(pDict.keys())[count]
        #decleration of variable to store tax
        tTax = 0
        if totals < 1000:
          tTax = totals * 0
        elif totals > 1000 and totals<= 10000:
          tTax = 1000 * 0
          totals = totals - 1000
          tTax = tTax + totals * 0.1
        elif totals > 10000 and totals <=20200:
          tTax = 1000 * 0
          tTax = tTax + 9000 * 0.1
          totals=totals-10000
          tTax = tTax + totals * 0.15
        elif totals >20200 and totals <= 30750:
          tTax = 1000 * 0
          tTax = tTax + 9000 * 0.1
          tTax = tTax + 10200 * 0.15
          totals=totals-20200
          tTax = tTax + totals * 0.2
        elif totals>30750 and totals<=50000:
          tTax = 1000 * 0
          tTax = tTax + 9000 * 0.1
          tTax = tTax + 10200 * 0.15
          tTax = tTax + 10550 * 0.2
          totals=totals-30750
          tTax = tTax + totals * 0.25
        else:
          tTax = 1000 * 0
          tTax = tTax + 9000 * 0.1
          tTax = tTax + 10200 * 0.15
          tTax = tTax + 10550 * 0.2
          tTax = tTax + 19250 * 0.25
          totals=totals-50000
          tTax = tTax + totals * 0.3
        dict_list.setdefault(names,tTax)
        count = count + 1
      return dict_list
    except(attributeError,TypeError):
      raise ValueError('The provided input is not a dictionary')
  else:
    print("only dict type values allowed")

用于测试我的代码是否有效的代码:

^{pr2}$

求你了帮助:-)在


Tags: andof代码dictionarycountdictlistvalues
1条回答
网友
1楼 · 发布于 2024-04-20 09:55:06

作为参考,来自失败测试的完整异常消息如下:

ERROR: test_it_does_not_accept_integers (__main__.CalculateTaxTests)
                                   
Traceback (most recent call last):
  File "calculate_tax_test.py", line 27, in test_it_does_not_accept_integers
    context.exception.message, "Invalid input of type int not allowed"
AttributeError: '_AssertRaisesContext' object has no attribute 'exception'

失败的测试是:

^{pr2}$

问题是calculate_tax之后的断言位置不正确。如果在calculate_tax中引发异常,将跳过断言。如果没有引发异常,断言将失败。因此,这一主张永远不会通过。在

修复方法是取消断言的缩进,将其移出with语句。为了清楚起见,我还插入了一行空白:

      def test_it_does_not_accept_integers(self):
        with self.assertRaises(ValueError) as context:
          calculate_tax(1)

        self.assertEqual(
          "The provided input is not a dictionary.",
          context.exception.message, "Invalid input of type int not allowed"
        )

如果调用with self.assertRaises(...)引发异常,with self.assertRaises(...)语句可以捕获异常。如果发生这种情况,异常详细信息将留在context,然后您的断言将能够测试异常是否如您所期望的那样。在

但是,即使进行了此更改,测试仍然失败,因为calculate_tax(1)不会引发ValueError。我让你来解决这个问题。在

相关问题 更多 >