简体   繁体   中英

Python AssertionError

This is what I am trying to do with my below code.

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.

Currently, I am receiving 2 errors:

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

Code:

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

Different value tests:

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.")

You are not assigning anything in __init__ . You appear to be making a series of comparisons ( != means not equal to ) with some tuples; because self.color doesn't exist yet, that then leads to the exception you see.

Assignment is = (and you do use assignment in the for_pies , so you know how to use this):

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

Note that the order of the arguments also needed adjusting there, and that I corrected the name of the variety attribute to match your requirements.

Next, your __str__ method will fail, because it uses several non-existing % formatters. Just use %s everywhere:

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

In other words, those are not the initial letters of the attribute names. I also added a comma between variety and grown .

Next, you can return the result of an in comparison directly, no need to use if...else :

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

Note that this method doesn't need to take variety_name as an argument! Your assignment tells you to use the self.variety instance attribute instead.

The completed class then is:

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")

You have to assign attributes of instance. Here is right code:

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

Change formatting:

    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)

Here change self.variety to self.variety_name :

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

Then you can write following assignments:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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