r/learnpython • u/ikeahotdog420 • 17h 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.')
6
u/crazy_cookie123 17h ago
You don't need to try to pull the variables out of the function, you can just create variables outside of the function and pass them in:
def total_seconds(hours, minutes, seconds):
return (hours*60*60) + (minutes*60) + seconds
h = 7
m = 21
s = 37
print(f'{h} hours, {m} minutes and {s} seconds have a total of {total_seconds(h, m, s)} seconds.')
1
u/ikeahotdog420 17h ago
Thank you! But I’m wondering whether there is a way to cram it all inside the function; so that all I’d have to do is enter the parameters and have them return as the printed statement (hope this makes sense haha)
3
u/socal_nerdtastic 17h ago
You could return the segment (aka string) to be printed.
def total_seconds(hours1, minutes1, seconds1): total = (hours1*60*60)+(minutes1*60)+seconds1 return f'{hours1} hours, {minutes1} minutes and {seconds1} seconds have a total of {total} seconds.') print(total_seconds(7,21,37))Edit: fixed your typo that didn't display minutes.
2
u/danielroseman 17h ago
You can do whatever you want within the function. So you can return the whole string if that's what you want?
def format_time(hours, minutes, seconds): total_seconds = (hours*60*60) + (minutes*60) + seconds return f'{h} hours, {m} minutes and {s} seconds have a total of {total_seconds} seconds.' print(format_time(h, m, s))3
u/seanv507 17h ago
Op, you can return a tuple (hours, minutes, seconds) , list [hours,...] or more complex structure.. Iookup python time class
https://docs.python.org/3/library/datetime.html#datetime.time
1
u/desrtfx but other languages pro 14h 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.
2
u/Temporary_Pie2733 17h ago
Do what? What three functions do you think you need? I think you are confused because you have two separate variables named hours1: the global variable that you use as an argument to total_seconds, and the local variable that you use as the parameter to total_seconds.
1
u/Rubix321 17h ago
I assume the print statement is supposed to be outside of the function. Format as a code block in reddit to make it clear.
Ultimately you want the arguments you're feeding into total_seconds(h,m,s) to be variables so that you can reference those variables to get your minutes and seconds in your print statement.
1
u/Adrewmc 17h 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)
1
u/MezzoScettico 17h ago edited 17h ago
Why do you need the function to tell you what the hours, minutes and seconds were? You must have known that in order to pass those things in as arguments to the function. Whatever you put in your call to total_seconds, put those things in your print statement. I'm confused about the use case here.
However, I will pass on what I was originally going to answer, which is that you can return multiple values separated by commas. They will be returned as a tuple.
def powers(x):
return x**2, x**3
y = 5
p = powers(y)
print(f'The square of {y} is {p[0]} and the cube is {p[1]}')
You might find that related to what you were trying to do.
Edit: Perhaps more readably than "p[0]", you can also use the comma to unpack tuples.
def powers(x):
return x**2, x**3
y = 5
sq,cu = powers(y)
print(f'The square of {y} is {sq} and the cube is {cu}')
So you could use that idea to return things in addition to the number of seconds.
1
-1
17h ago
[deleted]
1
u/Outside_Complaint755 17h ago
Don't use global variables unless absolutely necessary. It will just cause headaches and is definitely a code smell.
Plus, if you're returning it, then making it global is unnecessary.
9
u/danielroseman 17h ago
It's not at all clear what you are asking here. What three separate functions?