[Python] How to overpass test case on exercise in MOOC.fi?

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    Been studding python for a while now, did not ask anything since AI was useful for clarifying most of the simple stuff. In advancing Python programming course, in chapter 10, one of the last exercise was phonebook exercise as a final OOP exercise. My code has passed all of the test cases but on one I got stuck. I tried debugging for about 4h and could not figure out so I'm in doubt weather a test might be wrong since I'm not familiar with tests. Here is my entire OOP code followed by a test on which code fails with an output of a failed test. I would really apreciate if anyone could clarify my mistake.

    class PhoneBook:
    def __init__(self):
    self.__persons = {}

    def add_number(self, name: str, number: str):
    if name in self.__persons:
    self.__persons[name].add_number(number)
    else:
    self.__persons[name] = Person(name=name, numbers=[number])

    def get_entry(self, name: str):
    # IMPORTANT this part bellof (2-3 lines) is problematic for the given test case...
    if name in self.__persons and not self.__persons[name].address():
    print(self.__persons[name].numbers()[0])
    print("address unknown")
    elif name in self.__persons and not self.__persons[name].numbers():
    print("number unknown")
    print(self.__persons[name].address())
    elif name not in self.__persons:
    print("address unknown")
    print("number unknown")
    else:
    for num in self.__persons[name].numbers():
    print(num)
    print(self.__persons[name].address())


    def add_address(self, name:str, address:str):
    if name in self.__persons:
    self.__persons[name].add_address(address)
    else:
    self.__persons[name] = Person(name = name, address=address)

    def all_entries(self):
    return self.__persons

    class PhoneBookApplication:
    def __init__(self):
    self.__phonebook = PhoneBook()

    def help(self):
    print("commands: ")
    print("0 exit")
    print("1 add number")
    print("2 search")
    print("3 add address")

    def add_number(self):
    name = input("name: ")
    number = input("number: ")
    self.__phonebook.add_number(name, number)

    def search(self):
    name = input("name: ")
    self.__phonebook.get_entry(name)

    def add_address(self):
    name = input("name: ")
    address = input("address: ")
    if address:
    self.__phonebook.add_address(name, address)
    else:
    raise ValueError("Enter a valid address.")

    def execute(self):
    self.help()
    while True:
    print("")
    command = input("command: ")
    if command == "0":
    break
    elif command == "1":
    self.add_number()
    elif command == "2":
    self.search()
    elif command == "3":
    self.add_address()
    else:
    self.help()

    class Person:
    def __init__(self, name:str, address:str = None, numbers:list = None):
    self.__name = name
    self.__address = address if address is not None else ""
    self.__numbers = numbers if numbers is not None else []

    def name(self):
    return self.__name

    def numbers(self):
    return self.__numbers

    def address(self):
    if self.__address:
    return self.__address
    else:
    return None

    def add_number(self, number:str):
    if number:
    self.__numbers.append(number)
    else:
    raise ValueError("Enter a valid number.")

    def add_address(self, address:str):
    if address:
    self.__address = address
    else:
    raise ValueError("Enter a valid address.")

    def __str__(self):
    return f"{self.numbers()}\n{self.address()}"


    phonebook = PhoneBookApplication()
    phonebook.execute()



    TEST FAIL OUTPUT:

    #IMPORTANT TEST FAIL OUTPUT:
    # Test failed
    # PhoneBook2_Part2Test: test_3_works_many_numbers
    # The output of your program should be
    # 040-999999
    # with input
    # 1
    # Emilia
    # 09-123456
    # 1
    # Emilia
    # 040-999999
    # 2
    # Emilia
    # 0
    # Now the output was
    # commands:
    # 0 exit
    # 1 add number
    # 2 search
    # 3 add address
    #
    # 09-123456
    # address unknown


    TEST ON WHICH CODE FAILS:

    import unittest
    from unittest.mock import patch

    from tmc import points, reflect
    from tmc.utils import load, load_module, reload_module, get_stdout, check_source
    from functools import reduce
    import os
    import os.path
    import textwrap
    from random import choice, randint
    from datetime import date, datetime, timedelta

    def test_3_works_many_numbers(self):
    syote = ["1", "Emilia", "09-123456", "1", "Emilia", "040-999999", "2", "Emilia", "0"]
    with patch('builtins.input', side_effect=syote):
    try:
    reload_module(self.module)
    except:
    self.fail(f"Check that the program works with input\n{s(syote)}")

    output = get_stdout()
    expected = "09-123456"
    self.assertTrue(expected in output,
    f"The output of your program should be\n{expected}\nwith input\n{s(syote)}\nNow the output was\n{output}")
    expected = "040-999999"
    self.assertTrue(expected in output,
    f"The output of your program should be\n{expected}\nwith input\n{s(syote)}\nNow the output was\n{output}")

    Continue reading...

Compartilhe esta Página