コード例 #1
0
class Test_Lootbag(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.red_bag = Lootbag('red')
        self.Amy = Kid('Amy', 1)
        self.Bruce = Kid('Bruce', 0)
        self.red_bag.kids.add(self.Amy)
        self.red_bag.kids.add(self.Bruce)
        self.Amy.add_toy('bike')
        self.Amy.add_toy('ball')
        self.Amy.add_toy('calculator')

    def test_lootbag_exists(self):
        self.assertIsInstance(self.red_bag, Lootbag)
        self.assertEqual(self.red_bag.color, 'red')
        self.assertNotEqual(self.red_bag.color, 'green')

    def test_lootbag_has_kids(self):
        self.assertEqual(len(self.red_bag.kids), 2)
        self.assertNotEqual(len(self.red_bag.kids), 0)

    def test_add_toy_to_kid(self):
        self.assertIn('bike', self.Amy.toys)
        self.assertNotIn('car', self.Amy.toys)

    def test_remove_toy_from_kid(self):
        self.Amy.add_toy('doll')
        self.assertIn('doll', self.Amy.toys)
        self.Amy.remove_toy('doll')
        self.assertNotIn('doll', self.Amy.toys)

    def test_delivery(self):
        self.Amy.delivered = False
        self.Amy.deliver_all_toys()
        self.assertTrue(self.Amy.delivered)
コード例 #2
0
import sys
from lootbag import Lootbag
from kid import Kid

Yellow_Bag = Lootbag('yellow')
Candace = Kid('candace', 1)
Derek = Kid('derek', 0)
Yellow_Bag.kids.add(Candace)
Yellow_Bag.kids.add(Derek)
Candace.add_toy('crayons')

# Requirements
# 1. Add a toy: (3 args): the word "add", the name of the gift, the name of the child (lootbag.py add kite suzy)
# remove suzy kite
# list children currently receiving presents (using the ls command)
# list all toys for a specific child (ls suzy)
# mark all of a child's toys delivered (delivered suzy)

if len(sys.argv) > 1 and sys.argv[1] == "add":
    # add kite suzy
    # !This looks like it works, but it doesn't. Can't figure out why. It says the toy is added to the list, but printing the list shows that it isn't added. Unit testing shows that the toy DOES get added so IDFK.
    for k in Yellow_Bag.kids:
        if (sys.argv[3]).capitalize() == k.name:
            k.add_toy(sys.argv[2])
            k.toy_list()
elif len(sys.argv) > 1 and sys.argv[1] == "remove":
    # remove suzy kite
    # !Because the add command doesn't work, this one doesn't work. You can't remove a toy if it isn't in there in the first place.
    for k in Yellow_Bag.kids:
        if (sys.argv[2]) == k.name:
            k.remove_toy(sys.argv[3])
コード例 #3
0
ファイル: lootbag.py プロジェクト: MrErin/PythonExercises
        """Returns the list of deliveries by child"""
        print("----- Delivery List -----")
        for k in self.kids:
            if k.is_nice == True and k.toys and k.delivered == False:
                print(k.toy_list())
            if k.is_nice == False and k.delivered == False:
                print(f"*** {k.name}'s Toy List ***")
                print('Coal')

    def __str__(self):
        return f"This bag is colored {self.color}."


if __name__ == '__main__':
    Blue_Bag = Lootbag('blue')
    Amy = Kid('amy', 1)
    Bruce = Kid('bruce', 0)
    Blue_Bag.kids.add(Amy)
    Blue_Bag.kids.add(Bruce)
    Amy.add_toy('bike')
    Amy.add_toy('sailboat')
    Amy.add_toy('ball')

    print(Blue_Bag)
    Blue_Bag.kid_list()
    Blue_Bag.delivery_list()
    Amy.deliver_all_toys()
    Bruce.deliver_all_toys()
    Amy.deliver_all_toys()
    Bruce.deliver_all_toys()