这就是我尝试使用下面的代码。
Apple类应该采用4个构造函数输入:颜色(颜色),苹果品种名称(variety_name),大小(大小)以及它在(place_grown)中生长的位置。这些输入应分配给以下实例变量:color,variety,size,grown_in。
Apple类也应该有一个字符串方法,它应该返回 一个格式为
的字符串A <SIZE> <COLOR> apple of the <VARIETY> variety, grown in <PLACE GROWN IN>.
for_pies
方法应该返回布尔值True 品种是“Granny Smith”,“Braeburn”或“Golden Delicious”,如果 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.")
答案 0 :(得分:3)
您没有在__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)
换句话说,那些不是属性名称的首字母。我还在variety
和grown
之间添加了一个逗号。
接下来,您可以直接返回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")
答案 1 :(得分:2)
您必须分配实例的属性。这是正确的代码:
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