How to take something inside a function outside to global? ... and ... How did the Function call do that?

Please help. Much confusion in Step 13 here:

Here’s my code. And it worked! It printed out a list of the gamers who cannot attend on game_night (Thursday).

unable_to_attend_best_night = []
def not_available_on_night(gamers_list, day):
    for gamer in gamers_list:
        if not day in gamer['availability']:
not_attending_game_night = not_available_on_night(gamers, game_night)

But roadblock for me as I move on to the second, third, and fourth bullet points of the instructions set. I blindly followed the instructions to create this function and that, to blindly name this variable and that parameter…

second_night_availability = build_daily_frequency_table()
calculate_availability(unable_to_attend_best_night, second_night_availability)
second_night = find_best_night(second_night_availability)

Erm. The fourth bullet point says to Call find_best_night with the now filled-in second_night_availability, save the results in second_night.. But, um, pretty sure I did nothing to “fill in” second_night_availability. This newly named second_night_availability just happened. I didn’t code or do anything to this new name. So how and when did it magically fill in itself … and filled in with what?

Replying to my own post here. Can someone check that my thinking is correct?

My original questions were how was second_night_availability filled in and with what. The second bullet point in the instructions set addresses that and with the code second_night_availability = build_daily_frequency_table(). The output from the code that I did in Step 5 is what goes into second_night_availability.

Code from Step 5

def build_daily_frequency_table():
    return {
        "Monday": 0,
        "Tuesday": 0,
        "Wednesday": 0,
        "Thursday": 0,
        "Friday": 0,
        "Saturday": 0,
        "Sunday": 0
count_availability = build_daily_frequency_table()

The third bullet point puts all the gamers from ‘unable_to_attend_best_night’ (that is, everyone who is not available on Thursday the ‘game_night’) and filters them through ‘build_daily_frequencey_table’ (which I most recently renamed to ‘second_night_availability’) to determine how many gamers are available on the not ‘game_nights’ of Monday/Tuesday/Wednesday/Friday/Saturday/Sunday.

Help please:

  1. At this point, I want to print out ‘calculate_availability’ to see a list of those not ‘game_nights’ and the number of gamers that can attend. But when I print(calculate_availability), I get <function calculate_availability at 0x103ef5ab0>. Not helpful! What happened here?

UPDATE: I have to print’ second_night_availability’ because it is the second variable up in Step 7 that I plugged into the second parameter of the code in Step 6 which I was adding to. So many layers of coding to keep track of!

  1. I’m not seeing why, for the fourth bullet point, we are to put ‘second_night_availability’ as the parameter. Shouldn’t it be ‘count_availability’ like what I did in Step 9 (which is what the Codecademy Solution does too up in Step 9)?

UPDATE: Same reasoning as above?

Quick function and scope review.

>>> def foo (a, b, c):
...     return a * 2, b * 3, c * 4
>>> p, q, r = foo (2, 3, 4)
>>> p, q, r
(4, 9, 16)

In other words, to pass values into a function, send them as call arguments. Those values will be assigned to the function parameters and can be processed under those names. To pass values out of the function, use the return statement and assign it to variables in caller scope.


Thank you mtf. Your explanations are always so clear and easy for me to understand. I really appreciate your help.

1 Like