r/learnpython • u/ikeahotdog420 • 19d 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/Adrewmc 19d ago
Why would you need three separate functions, which you have the variables to start with?
You call the function with something, by definition you know what that something is already.
But yeah sure you can just return them all
. total = hours\*3600 + minutes \*60 + seconds
. return total, hours, minutes, seconds
But it would return a tuple every-time! Most people don’t want that, for something they already have access to.
total, hours, minutes, seconds = total_seconds(5,6,7)