#đź”’ Test case

23 messages · Page 1 of 1 (latest)

cobalt kelp
#
class Person:
    def __init__(self, name):
        self.name = name
        self.is_alive = True
        self.children = []

    def add_child(self, child):
        self.children.append(child)

    def get_children_names(self):
        for child in self.children:
            return[child.name for child in self.children]

    def dies(self):
        self.is_alive = False
        print("{} has died :(".format(self.name))

    def count_living_descendants(self):
        count = 0
        for child in self.children:
            if child.is_alive:
                count += 1 
                count += child.count_living_descendants()
        return count
patent juncoBOT
#

@cobalt kelp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cobalt kelp
#

How do I fix this failed test case?

pastel forge
#

Can you show the code on the screen? And is there any other output

glass mesa
cobalt kelp
#

No source code unfortunately, it only says “person should start of with no children”

glass mesa
#

Also, what do you think this program outputs?

def f(lists):
    for sub in lists:
        return [x + 1 for x in sub]

items1 = [[1, 2]]
items2 = [[1, 2], [3, 4], [5, 6]]
items3 = []

print(f(items1))
print(f(items2))
print(f(items3))
glass mesa
cobalt kelp
#
from person import Person

#####################
## Create a person ##
#####################
arthur = Person("Arthur")

#####################
## Adding children ##
#####################
arthur.add_child(Person("Bill"))
arthur.add_child(Person("Charlie"))
arthur.add_child(Person("Percy"))

# (We can add children like this too)
fred = Person("Fred")
arthur.add_child(fred)

george = Person("George")
arthur.add_child(george)

ron = Person("Ron")
arthur.add_child(ron)

ginny = Person("Ginny")
arthur.add_child(ginny)

#######################
## Count descendants ##
#######################
print(arthur.count_living_descendants())
# -> 7

######################
## List of children ##
######################
print(arthur.get_children_names())
# -> ['Bill', 'Charlie', 'Percy', 'Fred', 'George', 'Ron', 'Ginny']

##################
## People dying ##
##################
fred.dies()
# -> print "Fred has died :("

print(arthur.count_living_descendants())
# -> 6

#############################
## Adding more descendants ##
#############################
ron.add_child(Person("Rose"))
ron.add_child(Person("Hugo"))

ginny.add_child(Person("James"))
ginny.add_child(Person("Albus"))
ginny.add_child(Person("Lily"))

print(arthur.count_living_descendants())
# -> 11

print(ginny.count_living_descendants())
# -> 3
#

this is person_test.py

cobalt kelp
#

item 3 should be nothing right?

glass mesa
#

what do you mean by nothing?

cobalt kelp
#

there's nothing in the list so it should be none?

glass mesa
#

Yes, it's None. If a function finishes without hitting a return statement, it implicitly returns None

#

!e

def f(lists):
    for sub in lists:
        return [x + 1 for x in sub]

items1 = [[1, 2]]
items2 = [[1, 2], [3, 4], [5, 6]]
items3 = []

print(f(items1))
print(f(items2))
print(f(items3))
patent juncoBOT
#

@glass mesa :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [2, 3]
002 | [2, 3]
003 | None
glass mesa
#

Judging from the assignment, the get_children_names method should always return a list. But if you have no children, you're returning None, not a list

cobalt kelp
#

ohh I see, but I'm not sure how else I can return it as a list instead of None

pastel forge
#

Well you're merging two ways to make lists

dry pawn
# pastel forge Well you're merging two ways to make lists

byeah, get_children_names wants to iterate children with a for-loop first, but once it does, it cuts itself off and returns a new list comprehension

if it does not iterate children with a for-loop first, because there are none, then it returns None

to fix, just choose one way or the other, not both

patent juncoBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.