0

I am currently working through a small application. One of the requirements of the application is that I declare an empty list and pass the list as an argument to another function. This code works as expected, however, I am wondering if there is a more efficient or better way to accomplish this:

def main():
    HourlyTemperatures =  []
    GetTemperatures(HourlyTemperatures)

def GetTemperatures(HourlyTemperatures):

    for hour in range(0, 24):
        temps = input('Enter temperature value for hour %d: ' % (hour))
        HourlyTemperatures.append(temps)

        print(HourlyTemperatures)

if __name__ == "__main__":
    main()

Ideally, I think it would make more sense for readability (to me at least, and I am still very much a beginner!) for code that would look like this instead:

def main():
    HourlyTemperatures =  []
    GetTemperatures(HourlyTemperatures)

def GetTemperatures():

    for hour in range(0, 24):
        temps = input('Enter temperature value for hour %d: ' % (hour))
        HourlyTemperatures.append(temps)

        print(HourlyTemperatures)


if __name__ == "__main__":
    main()

If I leave off the argument on the GetTemperatures function, my code fails to run and I do not understand why or how to achieve a more desirable outcome.

Am I on the right path or is there a better way to do this?

digilink
  • 13
  • 1
  • In my honest opinion, i wouldn't structure it like that. Like having the empty list declared in the function that will be using it is how it usually should be. Also, having them invoked in another function called main wouldn't be so ideal. Sure, it helps with organizing and structuring but..you'd have to get use to structuring your code and associated functions in different files that'd you would import and call upon – penguin May 14 '20 at 22:23
  • You can't pass an argument to a function that doesn't accept an argument. They have to match. – Tom Karzes May 14 '20 at 22:23
  • 1
    People tried designing languages like what you want. Turns out it's a huge mistake. The way you want, it's harder to change what variable names you use, harder to tell what any particular name refers to, and harder to tell what the effect of using a particular variable name is. – user2357112 May 14 '20 at 22:28
  • 1
    (Well, actually, what I'm thinking of is dynamic scoping, which works a bit differently from what you've posted - the `GetTemperatures` call would just look like `GetTemperatures()` instead of `GetTemperatures(HourlyTemperatures) - but what you've posted has many of the same problems and a few new ones.) – user2357112 May 14 '20 at 22:31

1 Answers1

1

I think you want something like this:

def main():
    GetTemperatures()

def GetTemperatures(HourlyTemperatures=None):
    if not HourlyTemperatures:
        HourlyTemperatures = []

    for hour in range(0, 24):
        temps = input('Enter temperature value for hour %d: ' % (hour))
        HourlyTemperatures.append(temps)

        print(HourlyTemperatures)

if __name__ == "__main__":
    main()

It allows you to leave off the argument for GetTemperature (in which case the default one which evaluates to [] will be used), which is functionally equivalent to what you have now.

See this question for an explanation of why I used

if not HourlyTemperatures:
    HourlyTemperatures = []

I would make the variables names lowercase because of convention, but that's aside the question.

Mario Ishac
  • 5,060
  • 3
  • 21
  • 52