how can i use the output of one function to another function? - python

Let's assume I have two functions
def seq():
#here I wrote a code that evaluates the mean of a value from a csv file
print(x)#assuming the condition in the above code is true it prints x
seq()
and
def lenn():
p=4
d=#I want this variable to be the value that the 1st function produces
x=d/p
lenn()
One produces an integer and the other uses the output of the 1st function and then divides it with an integer to produce its own output. How do I call the function?
I tried calling the function name but when I tried to divide the function name with an integer it keeps saying that I have a None type. I also tried to put the 1st first function inside the 2nd function but I had the same problem.
How can i solve this?

Don't use print but return (print has no return value, so this defaults to None):
def seq():
return int(input())
def lenn():
p=4
d=seq()
x=d/p
return x
print(lenn())

The problem is that seq does not return the inputted value (x). Anyway, I wouldn't place int(input(x)) in its own function. You can try something like
def lenn():
p=4
d=int(input())
x=d/p
return x

Related

Get returned value of function in function which called it

I am newbie in python, and I build two functions in which, I am calling second function with 1 parameters in first function and I am trying to access second function's returned data in first function.
def second_function(first_param):
final = first_param + 50
return final
def first_function():
second_function(50)
# trying to access second_function's returned data HERE
print(second_function)
But it is not showing any returned data.
Any help would be much Appreciated. Thank You in Advance.
The problem here is that you are using print(second_function), so that will simply output the name of the function. Now, if you want to output the result of the function, you should do:
def second_function(first_param):
final = first_param + 50
return final
def first_function():
output = second_function(50)
print(output)
you could first put the returned value in a variable like this
def second_function(first_param):
final = first_param + 50
return final
def first_function():
value = second_function(60)
print(value )
or print the returned value with out using any variable
def second_function(first_param):
final = first_param + 50
return final
def first_function():
print(second_function(50))
That's because second_function is an object in its own right. Try either of the following:
def first_function():
out = second_function(50)
# trying to access second_function's returned data HERE
print(out)
def first_function_alternate():
print(second_function(50))
What's happening when you do print(second_function) is that the computer is trying to print the value of the function itself, not what it returns. We can store this value to a variable (my first answer) or simply generate it on-the-fly (my second answer).
In Python, the returned data from a function will be assigned to a variable. So you would use:
my_value = second_function(60)
and the returned value would be stored in the variable my_value

Store the return of a function in variable

I'm learning python, and I'm having trouble saving the return of a function to a specific variable. I have computed a simple function 'average' that is supposed to return the average value of a list. This works fine, however, when I try to store the result of average in a variable, I get told that x isn't defined.
def average(x):
return sum(x)/len(x)
var=average(x)
How do I store the return of the function in a variable?
Edit:
I misunderstood the task, which was simply to store the results of a specific computation in a variable.
x indeed is not defined
def average(x):
return sum(x)/len(x)
x = [1,2,3] # this was missing
var=average(x)
https://repl.it/join/eldrjqcr-datamafia
The function is a black box. You made the black box expect one mandatory input (x), therefore you have to provide it first i.e. var = average([1, 2, 3]).
Read the error message, x isn't defined. The variable x exists only in the average function and not in the program. You need to set x to something first.
e.g.
def average(x):
return sum(x)/len(x)
x=[1,2,3]
var=average(x)
this will not cause an error

How to call function in if statement and save return value

I'm stuck in a simple question that I can't find an answer.
If want to call a function in an if statement. This is a function really challenging and it takes a long time to get response, how can I preserve the return value?
I explain the problem with an example:
function
def recursive:
if .... :
return value
else:
return False
recursive is an hypothetically function that takes a lot of time to generating response, that could be a value or just a simple boolean False.
main
...
if recursive():
...value? (make something with value return)
other method
...
if recursive():
value = recursive()
This other method will call the function 2 times and it takes too long time.
How can I solve this?
Python 3.8 will add an operator to do exactly this (called the walrus operator :=), but unfortunately the closest thing you can do today is this
value = recursive()
if value:
pass # do stuff with value
else:
pass # do other stuff with value
# can still do stuff with value here
If running on 3.8+, the following will be valid
if value := recursive():
pass # do stuff with value
else:
pass # do other stuff with value
# can still do stuff with value here

Function not returning random integer for variable

I have a very simple problem, when I run the following code:
from random import randint
def create_random():
x = random.randint(1,4)
return x
print(create_random)
The output comes to this:
< function create_random at 0x7f5994bd4f28 >
Note: every character between "0x7f" and "f28" are random each time the code is run.
My goal was to be able to call the function multiple times with x being assigned a new integer value between 1 and 3 with each invocation.
You aren't actually calling the function. To do this you need to do:
print(create_random())
Just now you're printing the reference to the function which isn't very helpful for you in this case.
You have to call the function, like:
print(create_random())
Also in the function, this line:
x = random.randint(1,4)
Should be just:
x = randint(1,4)
Since you did a from ... import ... statement.
your last line does not do anything, since you want it to print 'create_random' that is not a variable. if you want to call a function, it has to have (). So, you should call it and put it in the print function:
print(create_random())

Input as a default value for a function

Is it possible to have input as a default value for a function?
For example,
f(x=input('>')):
print(x)
The problem is, it makes me input a value even when I supply an x value. How would I do this correctly?
Python will evaluate the default arguments you define for a function, you'll get the prompt during definition either you like it or not. The value entered at the input prompt will then be assigned as the default for x.
If you want a default value that will use input if no other meaningful value is provided, use the common idiom involving None and drop input in the body of the function, instead:
def f(x=None):
if x is None:
x = input('> ')
print(x)
Now, if you're thinking, I still want to somehow do this with default arguments, then you'll need to create a function that re-creates your function with every invocation:
def f():
def _(x = input('> ')):
print(x)
return _()
I don't see what benefit this would bring other than appeasing your curiosity, though :-)
May be this is what you want.
def f(x=None):
if not x:
x = input('>')
print(x)

Categories

Resources