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?