Pac-Man arcade game Task 3

def lose(has_eaten_all_dots, power_pellet_active, touching_ghost):
“”"Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet,
or if all dots have been eaten but the player touched a ghost.

:param has_eaten_all_dots: bool - has the player "eaten" all the dots?
:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - has the player lost the game?
"""
if has_eaten_all_dots and not power_pellet_active and touching_ghost:
    return True
if not has_eaten_all_dots and touching_ghost and not power_pellet_active:
    return True
return False

This is my current code and it works. However there frequently returns this error message: TypeError: lose() missing 1 required positional argument: ‘touching_ghost’.

I can’t find the mistake in my code since the ‘touching_ghost’ argument is included in the function definition.

Can someone please help me?

Can you share your entire solution? Which tests show this error? Please use codeblocks when sharing. Please do not use screenshots.

"""Functions for implementing the rules of the classic arcade game Pac-Man."""


def eat_ghost(power_pellet_active, touching_ghost):
    """Verify that Pac-Man can eat a ghost if he is empowered by a power pellet.

    :param power_pellet_active: bool - does the player have an active power pellet?
    :param touching_ghost: bool - is the player touching a ghost?
    :return: bool - can a ghost be eaten?
    """
    if power_pellet_active and touching_ghost:
        return True
    else:
        return False
    


def score(touching_power_pellet, touching_dot):
    """Verify that Pac-Man has scored when a power pellet or dot has been eaten.

    :param touching_power_pellet: bool - is the player touching a power pellet?
    :param touching_dot: bool - is the player touching a dot?
    :return: bool - has the player scored or not?
    """

    if touching_power_pellet or touching_dot:
        return True
    else:
        return False


def lose(has_eaten_all_dots, power_pellet_active, touching_ghost):
    """Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet,
    or if all dots have been eaten but the player touched a ghost.

    :param has_eaten_all_dots: bool - has the player "eaten" all the dots?
    :param power_pellet_active: bool - does the player have an active power pellet?
    :param touching_ghost: bool - is the player touching a ghost?
    :return: bool - has the player lost the game?
    """
    if has_eaten_all_dots and not power_pellet_active and touching_ghost:
        return True
    if not has_eaten_all_dots and touching_ghost and not power_pellet_active:
        return True
    return False
          

def win(has_eaten_all_dots, power_pellet_active, touching_ghost):
    """Trigger the victory event when all dots have been eaten and the player has not lost.

    :param has_eaten_all_dots: bool - has the player "eaten" all the dots?
    :param power_pellet_active: bool - does the player have an active power pellet?
    :param touching_ghost: bool - is the player touching a ghost?
    :return: bool - has the player won the game?
    """
    if has_eaten_all_dots and not (touching_ghost and not power_pellet_active):
        return True
    else:
        return False

Thanks for your reply. The test failures are 7, 8 and 9. The problem seems to be something in the lose() function.

Can you share the output of one of those tests? You may need to expand it to see the details. Please use a codeblock and not a screenshot.

These are the specific outputs.
Test 7:

actual_result = lose(True, False)
error_message = ('Called lose(True, False).'
                 f'The function returned {actual_result}, but the '
                 f'tests expected that the '
                 'player **does not** lose because they were '
                 'not touching a ghost.')

self.assertIs(actual_result, False, msg=error_message)

Test 8:

actual_result = lose(True, True)
error_message = ('Called lose(True, True).'
                 f'The function returned {actual_result}, but the '
                 f'tests expected that the '
                 'player **does not** lose because when they touched a '
                 'ghost, a power pellet was active.')

self.assertIs(actual_result, False, msg=error_message)

Test 9:

actual_result = lose(False, True)
error_message = ('Called lose(False, True).'
                 f'The function returned {actual_result}, but the '
                 f'tests expected that the '
                 'player loses because they touched a '
                 'ghost without a power pellet activated.')
self.assertIs(
    actual_result, True, msg=error_message)

The test failure is always the same: `TypeError: lose() missing 1 required positional argument: 'touching_ghost’´

Looking at the tests, do you understand what code the tests are trying to run?

Looking at your code, do you understand why the tests are falling?

The test messages want me to write the function so the apparently missing positional argument is included. However, what I don’t understand is why the apparently missing positional argument changes when I change the order of the positional arguments in the function definition.

def lose(has_eaten_all_dots, power_pellet_active, touching_ghost):
   if touching_ghost:
        return True
    else:
        return False
        
    if touching_ghost and power_pellet_active:
        return False

The test failure there is:

TypeError: lose() missing 1 required positional argument: 'touching_ghost'

When I change the order in the function to:

def lose(has_eaten_all_dots, touching_ghost, power_pellet_active):

The test failure returns

TypeError: lose() missing 1 required positional argument: 'power_pellet_active'

The error message in general remains the same as I already included in the previous messages. I don’t quite understand why hat is the case.

When you start the exercise, you’re given:

def lose(power_pellet_active, touching_ghost):
    """Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet.

    :param power_pellet_active: bool - does the player have an active power pellet?
    :param touching_ghost: bool - is the player touching a ghost?
    :return: bool - has the player lost the game?
    """

    pass

The code is complaining that the number of positional arguments passed by the tests (2) is short of the number of arguments expected by your code (3).

The tests expect the code to take a specific number of arguments in a specific order. If you change that in the code, the tests don’t automatically update how they call the function and they will be sad. You need to write your functions exactly how the tests call them.