2017-03-19 32 views
-2

這是我正在試圖用我的下面的代碼。Python AssertionError

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.' 

代碼:

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

'! ='是比較運算符,而不是賦值。改用'='。我不確定你爲什麼使用可能值的元組,你的任務並不要求你驗證值。 –

+0

@ Martijn Pieters偉大我帶走了!=,現在我收到錯誤Traceback(最近呼叫最後一個): 文件「Apple.py」,行136,在 print ap2#應該打印:一個大青蘋果在澳大利亞悉尼種植的格蘭尼史密斯品種 文件「Apple.py」,第123行,在__str__ %(self.size,self.color,self.variety_name,self.place_grown) TypeError:需要整數 –

+0

問題的標題與'AssertionErrors'有什麼關係? – martineau

回答

2

你沒有分配任何東西在__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") 
1

你必須指定實例的屬性。下面是正確的代碼:

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.varietyself.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 
+0

使用其他格式%s。我在 –

+0

以上寫過'for_pies',順便說一句,不需要論證。 –

+0

你說得對。對不起,我的錯誤 –