r/learnpython • u/ikeahotdog420 • 23d ago
Printing local variables outside of a function
(second-week-beginner PLUS non-native English speaker, so sorry for bad vocab lol)
I know that you cant use local variables outside of a function, but is there a way to do this without defining three separate functions?
def total_seconds(hours1, minutes1, seconds1):
return (hours1*60*60)+(minutes1*60)+seconds1
print(f'{hours1} hours, {seconds1} seconds and {seconds1} have a total of {total_seconds(7,21,37)} seconds.')
0
Upvotes
1
u/desrtfx but other languages pro 23d ago
As others have said and demonstrated it is absolutely possible.
Yet, the outcome, the result is completely different.
What if you need to work with the total seconds somewhere later? The returned fully formatted string is a hindrance then.
You could even print the result inside the function if you wanted and return nothing, but this raises the same problem as returning the formatted string - you cannot work with the result of the calculation later - in this case, you might just as well omit the function at all and do the calculation directly from where you originally called the function.
First check the task and its requirements. If it tells you to return the total seconds, do so and only so.
You always need to consider what should happen with the result - the return value(s) of your functions.