haha, a new question:
i have a function which currently returns either true or false.
I currently use it like
Problem is i now want the function to return a list of values ( as in "[1,2,3]")Code:if(function_name): ... ...
if the function returns true. I'm just wondering if this is possible, and if so, can anyone help me implement it?
thanks in advance![]()
Presumably your original function looks something like
To return a list all you need to do is replace True with the list you want returned if true, and False with the list you want returned if false. ieCode:def function_name(var): if (//some logic): return True; else return False;
You then have to assign the return value to a variableCode:def function_name(var): if (//some logic): return [1,2,3]; else return [4,5,6];
Code:list_var = function_name(somevar);
If there's a new way, I'll be the first in line.
But, it better work this time.
will that still let me use the return value as True?
does that make more sense, as to what it is im trying to do?Code:def function_name(): if(some_logic): return List_Of_Values else return False def new_function(): if(Function_name()): code which uses returned values else: quit()
or maybe:
would that work better?Code:def function_name(): if(some_logic): return List_Of_Values else return False def new_function(): if(var=Function_name()): code which uses var else: quit()
In python any non-zero or non-null variable is evaluated to be true.
So if you have
you can then useCode:def function_name(): if(some_logic): return List_Of_Values; else: return False;
which will evaluate to True if the list is returned by function_name (assuming it isn't an empty list which would evaluate to False) and will evaluate to False otherwise.Code:if (function_name()):
To use the returned values with in the if statement you would have to first assign the returned value to a variable such as
you could then re-write the previous if statement asCode:var = function_name();
Code:if (var):
If there's a new way, I'll be the first in line.
But, it better work this time.
ok, perfect, thx![]()
You do know that Python doesn't require parenthesis after an if statement, right?
You can very easily do this:
Also, what's with ending lines with semicolons? Further, Python cares about whitespace:Code:def function_name(): if some_logic: return List_Of_Values #; else: return False #;
Would need to be:Code:def function_name(): if(some_logic): return List_Of_Values else return False def new_function(): if(Function_name()): code which uses returned values else: quit()
The code you posted would have two empty functions named "function_name" and "new_function", and then random if statements afterward. Not what I think you wanted to to.Code:def function_name(): if some_logic: return List_Of_Values else: return False def new_function(): if Function_name(): code which uses returned values else: quit()![]()
Last edited by ZekeDragon; 10-12-2009 at 12:44 AM. Reason: first code set wasn't edited.
Wow I changed my sig!
haha, yes. i posted that off a mobile phone keypad.
EDIT: oops, no i didn't. haha, no excuses there![]()
I knew someone would pick up on the parenthesis after I posted it. Still, it's largely a taste and readability thing and, as Milo had posted his code like that, I figured that he is probably more comfortable with that style of code.
Good spot on the semicolons though. Those were the first lines of python code I've written in months. I seem to have got into the habit of ending all statements with a semicolon without even thinking about it.
If there's a new way, I'll be the first in line.
But, it better work this time.
**** semicolons
There is another problem here, it seems he wants to use the returned values, in Python you can't do like in C (if (x=foo()){some code here with x})
you need to declare x = foo() then if x:do something, else:...die you lousy computer
BUT, if you are sure you will use lists, you can use a for loop and make f return [] instead of False.
Here:
Code:def f(x): if iOwns: return [x,x*2,x*3,x*4] else: return [] for i in f(32): #do something...
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks