这就是我试图用下面的代码做的。在
The class Apple should take 4 constructor inputs: the color (color), the name of the apple variety (variety_name), the size (size), and the place it grew in (place_grown), in that order. Those inputs should be assigned to the following instance variables: color, variety, size, grown_in.
The class Apple should also have a string method, which should return a string of the format
A <SIZE> <COLOR> apple of the <VARIETY> variety, grown in <PLACE GROWN IN>.
The
for_pies
method should return the boolean value True if the variety is "Granny Smith", "Braeburn", or "Golden Delicious", and if the variety value for that Apple instance is anything else, the method should return the boolean value 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.'
代码:
^{pr2}$不同值测试:
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.")
你必须分配实例的属性。这是正确的代码:
更改格式:
^{pr2}$此处将
self.variety
改为self.variety_name
:然后你可以写下以下作业:
您没有在
__init__
中分配任何内容。您似乎正在与一些元组进行一系列比较(!=
表示不等于);因为self.color
还不存在,这将导致您看到的异常。在赋值是
=
(而且您确实在for_pies
中使用赋值,所以您知道如何使用它):注意,参数的顺序也需要调整,并且我更正了
variety
属性的名称以符合您的要求。在接下来,
^{pr2}$__str__
方法将失败,因为它使用了几个不存在的%
格式化程序。只需在任何地方使用%s
:换句话说,这些不是属性名的首字母。我还在
variety
和grown
之间添加了一个逗号。在接下来,您可以直接返回
in
比较的结果,不需要使用if...else
:注意,这个方法不需要将
variety_name
作为参数!分配告诉您使用self.variety
实例属性。在完成的课程是:
相关问题 更多 >
编程相关推荐