How can I assign an output to a function call in python? - function

I know I can't necessarily assign values to function call, so how would I assign an output to that function call? I've built a function that checks if a location (row,col) is valid in puzzle(list). I built another function that returns the value at that location. I am now building a third function that calls those two functions to set a value at a specific location, if that value was initially none. If the value was able to be set, it needs to return True. If the value was not None, it doesn't need to be set and it should return false.
For example:
set_location([[1]], 1, (0,0)) → False
set_location([[None]], 1, (0,0)) → True # puzzle is now [[1]]
My code is the following:
def is_valid_location(loc,puzzle):
x,y=loc
return x in range(len(puzzle[0])) and y in range(len(puzzle))
def get_value_at_location(puzzle,loc):
val_loc=puzzle[loc[0]][loc[1]]
return val_loc
def set_location(puzzle,value,loc):
if is_valid_location(loc,puzzle)== True:
if get_value_at_location(puzzle,loc) != None:
return False
else:
if get_value_at_location(puzzle,loc) == None:
get_value_at_location(puzzle,loc)= value
return True
Obviously the problem is this line:
get_value_at_location(puzzle,loc)= value
But I'm not sure how else to do it. Any suggestions?

Would your solution be:
value = get_value_at_location(puzzle,loc)

Related

Function that uses some of its variables only conditionally

Suppose f(x,y) is a bivariate function defined along the following lines. So f uses the second variable only conditionally.
def b
.
.
.
def f(x,y):
if b == True
return (x,1)
else
return (x,y)
I want to rewrite f as a univariate function of x which at a later stage calls a function which takes y, but only if necessary!
So I want to curry and write something along the following lines.
def g(x):
if b == True
return (x,1)
else
return f(x,?)
Where f(x,?) is itself the function i_1x(y)=(x,y).
So I want to only inject the second variable conditionally. Evidently g returns two different types, which is likely a problem. We can resolve this artifially by having the first condition returns the function p_1(y)=(x,1), but that would return a function which would still ask for another input.

Function does not deliver the desired values in python

I am trying to use both functions below:
def func1():
print('blahblah')
func2()
def func2():
check = 0
while check < 1:
first = input('Integer or pass ')
if first == 'pass':
first = 0
func1()
break
else:
first = int(first)
second = input('Integer or pass')
if second == 'pass':
second = 0
func1()
break
else:
second = int(second)
third = input('Integer or pass' )
if third == 'pass':
third = 0
func1()
break
else:
third = int(third)
check = 1
return first, second, third
The func2 returns None instead of the inputs when once "pass" was entered. What am I doing wrong?
edit: the results should be 3 integers, no matter how many times 'pass' was entered.
After changing the indentation I get following error: UnboundLocalError: local variable 'second' referenced before assignment (in case I start with pass)
For clarifcation: func2 asks for input, if input is "pass" then func1 is called.
After the print func1 calls func2 again . This repeats until 3 integers are input. Their valus shall be returned in th end.
Your indentation is wrong. You need to move the last statement return first, second, third one tab before.

How to call a function with less arguments that is set (Python 3)

I am making a terminal emulator in Python 3. The commands are being stored in functions, like:
def rd(os_vartmp, os_vartmp2):
if os_vartmp == None:
print('rd [path] [-S]')
print('Delete a folder')
else:
if os.path.isfile(os_vartmp) == True:
if os_vartmp2 == '-S': print('a')
else:
print(ERR5)
a = input('Command: ')
The terminal works like this:
Asks user for input
Splits the input
Uses the first part of input to search a function in locals
If there is one, uses the rest part of input as argument
Calls the function
The thing here is, when i call the function 'rd' with, for example, 'rd "boot.py" -S' it works just fine. But if i need to call it like this: rd "boot.py", it throws me a error about 1 argument given when 2 are required. Is there a fix for that?
You can make an argument optional by assigning a value in the method definition. For example:
def Add(x=0, y=0):
return x+y
If you input only one value, y will default to 0. If I wanted to give y a value but have x fall back on it's default value I could do Add(y=10). I hope this helped!
Have you tried this?
def rd(os_vartmp, os_vartmp2="-S"):
Instead of trying to get null value, which would require rd("boot.py",null), you can ser default value and then you can do rd("boot.py").
Hope it works.

Getting variable from a function

function isEven(x)
print("Checking if "..x.." is even.\nWill return state as 1 if true.")
if math.fmod(x, 2) == 0 then
state = 1
end
return state
end
I know that I can just run isEven and then use the state variable. But is there a way to do it in one line?
Like isEven(8).state?
Any and all help is appreciated.
As Egor said in a comment, this is precisely what return values are meant to do. When you see a function call in your code, such as isEven(8), it evaluates into that function's return value.
function isEven(x)
print("Checking if "..x.." is even")
return (math.fmod(x, 2) == 0)
end
print( isEven(8) )
print( isEven(7) )
if isEven(8) then
print("a")
else
print("b")
end
Finally, I would just like to point out a couple of things about the isEven function: First of all if you want you could use the % operator instead of math.fmod. Secondly, in the example I used the function returns a boolean value (true or false) instead of a number (0 or 1).

Django BooleanField accepted values

I have a MySQL database where data are migrated from an Access database.
The problem is that access saves boolean true value as -1, while django saves boolean true value as 1 (as tipically happens with MySQL).
So, for boolean fields, old true values are saved as -1, while new true values are saved as 1.
I need to say to django to consider True both 1 and -1 for all boolean fields.
How can I do?
Thanks in advance,
Sabrina
Just update all of the old values to 1:
UPDATE <table>
SET <fieldname>=1
WHERE <fieldname>=-1
I don't have a clue what Django is, but if you did all your Boolean tests with NOT FALSE (or <>0) instead of with TRUE, it will work regardless of the actual value encoded for TRUE (-1 or 1).
Create a custom BooleanField class that extens from models.BooleanField. In the next class, true values are saved with -1 on DB.
class AccessCompatibleBooleanField(models.BooleanField):
def to_python(self, value):
if value == True:
return -1
if value == False:
return False
if value in ('t', 'True', '1', '-1'):
return -1
if value in ('f', 'False', '0'):
return False
raise exceptions.ValidationError(self.error_messages['invalid'])
If you want make filters like .filter(visibles=True) and visibles is custom boolean field, you have to add following method to your custom class.
def get_prep_value(self, value):
if value is None:
return None
b = bool(value)
if b:
return -1
return b