繁体   English   中英

Python AssertionError

[英]Python AssertionError

这就是我要用下面的代码做的事情。

Apple类应采用4个构造函数输入:颜色(颜色),苹果品种的名称(variety_name),大小(size)及其生长的位置(place_grown)(按此顺序)。 这些输入应分配给以下实例变量:颜色,品种,大小,growd_in。

Apple类也应该有一个字符串方法,该方法应该返回格式如下的字符串

 A <SIZE> <COLOR> apple of the <VARIETY> variety, grown in <PLACE GROWN IN>. 

如果变体为“ Granny Smith”,“ Braeburn”或“ Golden Delicious”,则for_pies方法应返回布尔值True;如果该Apple实例的变体值为其他值,则该方法应返回布尔值False。

目前,我收到2个错误:

Traceback (most recent call last):
File "Apple.py", line 312, in test_apple9
self.assertEqual(ap2.__str__(), "A large yellow apple of the Golden Delicious variety, grown in the United States.")
AssertionError: 'A large yellow apple of the Golden Delicious variety, grown in the United States ' != 'A large yellow apple of the Golden Delicious variety, grown in the United States.'

Traceback (most recent call last):
File "Apple.py", line 315, in test_apple10
self.assertEqual(ap2.__str__(),"A medium red apple of the Braeburn variety, grown in WA.")
AssertionError: 'A medium red apple of the Braeburn variety, grown in WA ' != 'A medium red apple of the Braeburn variety, grown in WA.'

码:

class Apple:
    def __init__(self, color, variety_name, size, place_grown):
        self.color = color
        self.variety = variety_name
        self.size = size
        self.place_grown = place_grown

    def __str__(self):
        return "A %s %s apple of the %s variety, grown in %s ." % (
            self.size, self.color, self.variety, self.place_grown)

    def for_pies(self):
        return self.variety in ("Granny Smith", "Braeburn", "Golden Delicious")

    ap2 = Apple("green","Granny Smith","large","Sydney, Australia")
    print ap2 # Should print: A large green apple of the Granny Smith variety, grown in Sydney, Australia
    print ap2.for_pies() # should print True

    ap3 = Apple("red","Mystery variety", "small","Michigan")
    print ap3.for_pies() # should print False
    print ap3 # should print: A small red apple of the Mystery variety variety, grown in Michigan

不同的价值测试:

import unittest

class Problem4(unittest.TestCase):
    def test_apple10(self):
        ap2 = Apple("red","Braeburn","medium","WA")
        self.assertEqual(ap2.__str__(),"A medium red apple of the Braeburn variety, grown in WA.")
    def test_apple9(self):
        ap2 = Apple("yellow","Golden Delicious","large","the United States")
        self.assertEqual(ap2.__str__(), "A large yellow apple of the Golden Delicious variety, grown in the United States.")

您没有在__init__分配任何内容。 您似乎正在对一些元组进行一系列比较( !=表示不等于 ); 因为self.color还不存在,所以会导致您看到异常。

分配为= (并且您确实在for_pies使用了分配,因此您知道如何使用它):

def __init__(self, color, variety_name, size, place_grown):
    self.color = color
    self.variety = variety_name
    self.size = size
    self.place_grown = place_grown

请注意,参数的顺序也需要在此处进行调整,并且我更正了variety属性的名称以符合您的要求。

接下来,您的__str__方法将失败,因为它使用了几个不存在的%格式化程序。 随处使用%s

def __str__(self):
    return "A %s %s apple of the %s variety, grown in %s " % (
        self.size, self.color, self.variety, self.place_grown)

换句话说,这些不是属性名称的首字母。 我还在varietygrown之间添加了逗号。

接下来,你可以返回结果的in比较直接,无需使用if...else

def for_pies(self):
    return self.variety in ("Granny Smith", "Braeburn", "Golden Delicious")

请注意,此方法不需要以variety_name作为参数! 您的分配告诉您改用self.variety实例属性。

然后,完成的类为:

class Apple:
    def __init__(self, color, variety_name, size, place_grown):
        self.color = color
        self.variety = variety_name
        self.size = size
        self.place_grown = place_grown

    def __str__(self):
        return "A %s %s apple of the %s variety, grown in %s " % (
            self.size, self.color, self.variety, self.place_grown)

    def for_pies(self):
        return self.variety in ("Granny Smith", "Braeburn", "Golden Delicious")

您必须分配实例的属性。 这是正确的代码:

class Apple():
    def __init__(self, color, variety_name, size, place_grown):
        self.color = color
        self.variety_name = variety_name
        self.size = size
        self.place_grown = place_grown

更改格式:

    def __str__(self):
        return "A %s %s apple of the %s variety grown in %s " \
            %(self.size,self.color,self.variety_name,self.place_grown)

在这里将self.variety更改为self.variety_name

def for_pies(self):
        return self.variety in ["Granny Smith", "Braeburn", "Golden Delicious"]

然后,您可以编写以下任务:

ap2 = Apple("green","Granny Smith","large","Sydney, Australia")
print ap2 # Should print: A large green apple of the Granny Smith variety, grown in Sydney, Australia
print ap2.for_pies() # should print True

ap3 = Apple("red","Mystery variety", "small","Michigan")
print ap3.for_pies() # should print False
print ap3 # should print: A small red apple of the Mystery variety variety, grown in Michigan

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM