+ 2

finally Function in Python

Can I use the "finally" function in python, even if it is outside of a "try" function? I'm wanting to use it to get a piece of code to print AFTER a return statement.

24th Jan 2018, 8:27 PM
ParadoxRegiment
ParadoxRegiment - avatar
21 Answers
+ 4
No... Try, Catch and Finally have a specific architecture and dynamic...
8th Aug 2021, 3:12 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
Short answer: no
24th Jan 2018, 8:32 PM
Jeremy
Jeremy - avatar
+ 2
def yes_no(): num = int(input("")) if num in range(0, 100000000) return remainder() if remainder() == 0 print ("This is an even number.") else print ("This is an odd number.") if num not in range(0, 1000000000) print ("Number is too high or too low") def remainder(): remainder = x % 2 print(yes_no()) I get a syntax error which refers to the range statement
24th Jan 2018, 10:46 PM
ParadoxRegiment
ParadoxRegiment - avatar
+ 1
You cannot use finally as it basically works with try and except blocks. For eg. The principal of if and else statements!
13th Mar 2022, 3:54 AM
SAIF ALI
SAIF ALI - avatar
+ 1
The thing I saw was that you called the second function in the first function while second function is not defined
12th Jul 2022, 2:15 PM
Ramtin Jafari
Ramtin Jafari - avatar
0
Alrighty. Sucks that I can't, but eh. I'll figure out another way to what I'm trying to do
24th Jan 2018, 10:22 PM
ParadoxRegiment
ParadoxRegiment - avatar
0
just print whatever you wanna print after calling the method/function
24th Jan 2018, 10:23 PM
Jeremy
Jeremy - avatar
0
The problem is that I'm trying to get the code to return a second defined function, and then see if that code is true or not, then print a line if it is true.
24th Jan 2018, 10:30 PM
ParadoxRegiment
ParadoxRegiment - avatar
0
in ur first function call the second function, check if true, print, return function one
24th Jan 2018, 10:39 PM
Jeremy
Jeremy - avatar
0
can you post it as code?
24th Jan 2018, 10:49 PM
Jeremy
Jeremy - avatar
25th Jan 2018, 12:37 AM
ParadoxRegiment
ParadoxRegiment - avatar
0
Ou cant have an elif after an else statement. also remove the ':' after elif. switch the elif and else around and it works
25th Jan 2018, 9:36 AM
Jeremy
Jeremy - avatar
0
It works if I do that, but it also still has "None" in the output. Plus, if you do a negative number the output gives a RunTime error
25th Jan 2018, 2:45 PM
ParadoxRegiment
ParadoxRegiment - avatar
0
the none comes because you are printing whatever yes_no() returns, which is none
25th Jan 2018, 2:48 PM
Jeremy
Jeremy - avatar
0
you get an runtime error because you are comparing your variable 'remainder' to 0 but this variable has never been created
25th Jan 2018, 2:49 PM
Jeremy
Jeremy - avatar
0
Not sure why the return for yes_no() is none, but that at least explains why I'm getting None. As for the second part, is it doing that because if remainder doesn't go into effect, the return still runs but it fails because there's no value?
25th Jan 2018, 3:36 PM
ParadoxRegiment
ParadoxRegiment - avatar
0
you never return anything therefore its none you create remainder in an if-block which doesnt execute if u chose a negativ number, so if your number is negativ you have no variable called remaineder. you still try to access it tho
25th Jan 2018, 3:42 PM
Jeremy
Jeremy - avatar
0
No.
20th May 2021, 8:19 PM
Dane Cupovic
0
๐Ÿ‘Œ๐Ÿฝ
9th Sep 2021, 2:47 AM
Hamed
Hamed - avatar
0
Never. whatever is writtin In local defining funcion, any statement, including finally doesn't work afer return statement. Thus, THE ANSWER IS NO.
27th Sep 2021, 2:14 PM
Siriki Lohit
Siriki Lohit - avatar