Function does not deliver the desired values in python - function

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.

Related

Return function

i want more understand about return function. Is return function the function have a return value,what is that mean ? Plis give example and explanation about function have a return value and not have a return value.
return is a statement for functions, so let's say you have a bit of code like print(myFunction()).
myFunction's return value is what print is printing.
so if I have
# define function
def myFunction():
# this gives "Hello World" to whatever runs it
return "Hello World"
# put the return value in a variable
a = myFunction()
# print the variable data
print( a )
when you run a = myFunction the function is returning "Hello World" and storing it in the variable a. then you're just printing value of a.
Think of it this way,
Sometimes you want your function to do something for example add something to a variable:
a = 1
def add_1(x):
x += 1
# This would add 1 to the variable a. No return needed.
add_1(a)
# Now a is 2.
but sometimes you would want a function to return something that you could work with. For example printing it or saving it to another variable:
a = 1
def add_1(x):
return x + 1
b = add_1(a)
# The function returns a + 1, which is 2, and saves it to the variable b.
print (add_1(b))
# The function prints the returned value of b + 1, which is 3 and would print 3.
Hopefully this was helpful.

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.

Passing Arguments within If Statement

I'm trying to pass arguments between two files, and am encountering issues. I'm trying to parse a message for the word 'foo' in it, and create a function that will check if the message is only 'foo' or perhaps is a word like 'foot', which contains foo but isn't the word foo. Here's the two files
test2.py
import os, sys
from functiontest import function
message = 'foo'
check = 0
if 'foo' in message:
function(message,check)
print(check)
print('bar')
else:
check = 0
if check == 0:
print('foo not recognized')
and the function file
functiontest.py
import os, sys
def function(a,b):
print('checking message')
a = a.split()
print(a)
if a[0] == 'foo':
b = 1
print(b)
return b
else:
b = 0
return b
When run, it indicates that when b is set to 1 and passes it, it doesn't get passed correctly and remains 0. I want it to pass the argument check to be 1 if it is detected that the word isn't exactly 'foo' so that the message will appear saying that 'foo is not detected'. What am I doing wrong?
Follow up question: Once check is confirmed as 0 within the if statement, is there a way to break the statement and not execute the next lines that are within that if statement and rather skip to the else statement? I would prefer to include this somehow in the function to make the main code look cleaner, because I could include more embedded if statements but I want to avoid that if possible.
You're throwing away the return value of function, then printing check, which was never changed from the original value of 0. I believe your intent was to reassign check with the return value of the function:
check = function(message,check)
print(check)

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

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)

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).