[Python] How to fix map function not working with a nested function

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    In the line of code below, the map function call does not seem to be updating the modulo_sum variable, and upon adding a print statement into the function that map is supposed to call, I realised the function is not being called? any ideas as to why? I have tried using vscode's debugger, but cant seem to find the solution. Does this relate to how the map function works?


    from itertools import repeat

    def getTotalX(a, b):
    # Write your code here
    joint_arr = a + b
    # print(joint_arr)
    factor_arr = []
    modulo_sum = 0

    def divide_number(number, factor):
    nonlocal modulo_sum
    print('here')
    if number > factor:
    mod_op = number % factor
    modulo_sum += mod_op
    print(modulo_sum)
    return mod_op
    mod_op = factor % number
    modulo_sum += mod_op
    print(modulo_sum)
    return mod_op

    for i in range(100):
    map(divide_number, joint_arr, repeat(i))
    if modulo_sum == 0:
    factor_arr.append(i)
    modulo_sum = 0

    return len(factor_arr)


    Example

    getTotalX([2, 4], [16, 32, 96])

    # result should be 3



    The map function is supposed to call the function that divides each element by a factor, and then update a nonlocal variable.

    Continue reading...

Compartilhe esta Página