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
#đź”’ Test case
23 messages · Page 1 of 1 (latest)
@cobalt kelp
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.
Closes after a period of inactivity, or when you send !close.
How do I fix this failed test case?
Can you show the code on the screen? And is there any other output
Do you have the source code for the test cases?
No source code unfortunately, it only says “person should start of with no children”
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))
The problem says "See the person_test.py file for a set of tests"
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?
what do you mean by nothing?
there's nothing in the list so it should be none?
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))
@glass mesa :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [2, 3]
002 | [2, 3]
003 | None
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
ohh I see, but I'm not sure how else I can return it as a list instead of None
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
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.