Output of a function used as an input for another [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Last question about this topic.
I didn't add the output as its not the issue rather then passing of the output. Basically the final output of each function is a list. I am running into a problem. So this is the code I have
def mean(studentp_file):
li = []
for line in studentp_file:
nameless = line[1:14]
for l in range(len(nameless)):
answer = sum(nameless)/len(nameless)
li.append(answer)
li.insert(0,"Needie Seagoon")
li.insert(2,"Eccles")
li.insert(4,"Bluebottle")
li.insert(6,"Henry Crun")
li.insert(8,"Minnie Bannister")
li.insert(10,"Hercules Grytpype-Thynne")
li.insert(12,"Count Jim Moriarty")
li.insert(14, "Major Dennis Bloodnok")
mean_li = li
return mean_li
passing_file = normalise("DB.csv.", "units.csv")
mean(passing_file)
"""
This function will print out the final mean average percentile for each student over
their computer science degree.
"""
def final(mean_li):
~ wanted to see if the code worked.~
print(mean_li)
mean_list = mean(studentp_file)
final(mean_list)
The problem I am having is passing the variable mean_li into the new function final(). I get the error mean_list = mean(student_file) is not defined? I was able to pass output of another function into the previous function but for some reason I can't do it here.
Any help would be great.

I guess the error is studentp_file is not defined. The variable studentp_file is only valid inside the definition of function mean. Outside of that function, you are using passing_file variable. This should work
mean_list = mean(passing_file)

Related

my question is how to take input from user in below code.. i got error in this code (missing 1 required positional argument) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
my question is how to take input from user in below code.I got error in this code (missing 1 required positional argument).
First consider copy and paste the code here instead of sending a link to image.
Then, your problems is that you are not passing the variable you called balance inside your functions that need to have a parameter.
so in line 19 instead of writing cust.deposit(), write this instead cust.deposit(balance) or cust.deposit(55).
Of course you need first to create an object of account in the right way. i.e. creating it with parameters it needs and that you defined in the class. e.g.
cust = Account(acno, name, balance)
and then: have your functions, e.g.:
cust.deposit(55)
You need to pass your variables as arguments to Account() as they are expected in the init.
Try
cust1 = Account(acno, name, balance)
and then
cust1.deposit(50) # or any other amount

Moving turtle after mouse click [duplicate]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this function:
def a(one, two, the_argument_function):
if one in two:
return the_argument_function
my the_argument_function looks something like this:
def b(do_this, do_that):
print "hi."
Both of the above are imported to a file "main_functions.py" for my ultimate code to look like this:
print function_from_main(package1.a, argument, package2.b(do_this, do_that)
The "if one in two" from "a"function works but "b"function still executes when being passed to "function_from_main" without waiting the check from "a" to see if it actually should execute.
What can I do?
package2.b(do_this, do_that) is a function call (a function name followed by parenthesis). Instead you should be passing only the function name package2.b the function a
You will also need to modify function a such that function be is called when the condition is satisfied
# function a definition
def a(one, two, the_argument_function, argument_dict):
if one in two:
return the_argument_function(**argument_dict)
def b(do_this, do_that):
print "hi."
# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value})

How to update a variable inside a function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a question about two different ways of writing a piece of code. I want to know whether they are both okay or one is better under some conditions? Basically, is it better to give the variable we want to update to the function or not?
def f1(num):
output.append(num)
output = []
f1(2)
print(output)
and
def f1(num, output):
output.append(num)
output = []
f1(2, output)
print(output)
In the first example, your function works for only adding element to globally defined certain array. And it is not good approach, you cannot use it for another array.
Second one has generic approach which is better. But only one small correction; you have an array named output, and you pass it to your function, but you keep its name same in your function. So, for your function, there are two output one global and one local, better use different names in this case:
output = []
def f1(num, arr):
arr.append(num)
f1(2, output)
print(output)
Please see warning PyCharm shows in same naming case:
Consider avoiding to use the first example where possible: global variables can be very difficult to work with, generating problems you never find easily. Instead, use the second piece of code.
You could also write something like the following code:
output = []
def add(num, listName):
listName.append(num)
return listname
for _ in range(5):
output = add(_, output)
print(output)

How to pass function as argument without executing it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this function:
def a(one, two, the_argument_function):
if one in two:
return the_argument_function
my the_argument_function looks something like this:
def b(do_this, do_that):
print "hi."
Both of the above are imported to a file "main_functions.py" for my ultimate code to look like this:
print function_from_main(package1.a, argument, package2.b(do_this, do_that)
The "if one in two" from "a"function works but "b"function still executes when being passed to "function_from_main" without waiting the check from "a" to see if it actually should execute.
What can I do?
package2.b(do_this, do_that) is a function call (a function name followed by parenthesis). Instead you should be passing only the function name package2.b the function a
You will also need to modify function a such that function be is called when the condition is satisfied
# function a definition
def a(one, two, the_argument_function, argument_dict):
if one in two:
return the_argument_function(**argument_dict)
def b(do_this, do_that):
print "hi."
# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value})

python .apply calling function error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
def awesome_count():
if 'awesome' in dict:
return 1
else:
return 0
products['awesome']= products['word_count'].apply(awesome_count)
TypeError: awesome_count() takes no arguments (1 given)
what is the issue with calling the function. can somebody help?
Looks like your awesome_count function should take one argument, dict:
def awesome_count(dict):
....
You should really call it something besides dict though as that is a built-in data type. For something simple like this d would be fine.
Another thing to keep in mind is that Python is not javascript -- there is no apply method on dicts unless you have subclassed and added it yourself.

Categories