Invoking python functions [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 2 years ago.
Improve this question
Trying to invoke the pay. I am doing something wrong on the "computepay()" invocation?
hrs = input("Enter Hours:")
hours = float(hrs)
def computepay():
p = computepay((hours>=40*10.5) + (hours>40*(10.5*1.5))
computepay()
pay = float(p)
print("Pay",pay)

Your overall code logic doesn't make sense. Your function declaration and definition - the way you call the function inside the function don't match.
def computepay():
The above code says computepay() doesn't take any argument.
p = computepay((hours>=40*10.5) + (hours>40*(10.5*1.5))
In this line of code, you are trying to call the function itself, but with arguments which it doesn't accept and hence you are getting the error.
Apart from this, neither does the argument that you are passing on that line makes much sense. You have called computepay() with argument - (hours>=40*10.5) + (hours>40*(10.5*1.5) but that won't work. (hours>=40*10.5) and (hours>40*(10.5*1.5) will evaluate to booleans and you can't add two boolean values.
I don't know what the logic is that you are trying to implement, but these are some major and obvious faults in your code that I could find.

def computepay():
This function does not accept any parameters and has not been overloaded in any point of the code, so
computepay((hours>=40*10.5) + (hours>40*(10.5*1.5))
will not work. You can overload it in another section of the code as:
def computepay(argument):
but unless you do it so, it will never work. In addition
(hours>=40*10.5)
this is a boolean value meaning that it is either true or false. You are trying to sum 2 boolean values, that also does not make sense. As far as I can see you are trying to implement recursion, hence your function invokes an instance of itself (or another unimplemented overloaded version) that will also not work because you do not have a base case. For more information: https://www.youtube.com/watch?v=nxiObVwQ8MM

Related

Understanding Python decorators [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I wrote a small piece of code to run it myself and understand the flow:
def perform_arithmetic_ops(func):
print("INSIDE PERFORM ARTITHMETIC")
def decorate(*args):
print("I JUST GOT DECORATED")
print(f"SUM -> {args[0]+args[1]}")
func(args[0],args[1])
return decorate
#perform_arithmetic_ops
def print_me(x,y):
print(f"X = {x} ; Y = {y}")
print_me(5,4)
Please correct me if my understanding, which I've summarized in the form points below, is incorrect.
def perform_arithmetic_ops is the decorator which only takes a callable object, in this case a function as its argument, correct?
The inner function def decorate() here is what actually performs the desired modification. What I noticed was commenting func(args[0],args[1]) inside def decorate() basically meant that the X={x} ; Y={y} statement wasn't being printed. So does this mean that the the decorator replaces everything the original function does? If yes, how is that a modification? Isn't it more of a replacement for another function?
return decorate() i.e. calling decorate() instead of returning it led to errors for tuple index out of range. Why?
Take a good look at this enormous answer/novel. It's one of the best explanations I've come across.
def perform_arithmetic_ops is the decorator which only takes a callable object, in this case a function as its argument, correct?
Yes. You are right about it.
The inner function def decorate() here is what actually performs the desired modification. What I noticed was commenting func(args[0],args[1]) inside def decorate() basically meant that the X={x} ; Y={y} statement wasn't being printed. So does this mean that the the decorator replaces everything the original function does? If yes, how is that a modification? Isn't it more of a replacement for another function?
A decorator is a function that takes a function as its only parameter and returns a function. This is helpful to “wrap” functionality with the same code over and over again. It is wrapping but not modification.
return decorate() i.e. calling decorate() instead of returning it led to errors for tuple index out of range. Why?
To understand decorators, you must first understand that functions are objects in Python.
Because you might be calling it without any parameters. Try this return decorate(5,1). But after this statement and if you try to decorate function like print_me, then print_me might be holding non-callable object. See this:
def perform_arithmetic_ops(func):
print("INSIDE PERFORM ARTITHMETIC")
def decorate(*args):
print("I JUST GOT DECORATED")
print(f"SUM -> {args[0]+args[1]}")
return func(args[0], args[1])
return decorate(5, 1)
#perform_arithmetic_ops
def print_me(x,y):
print(f"X = {x} ; Y = {y}")
return 5
# print_me(5,2) # Will throw: TypeError: 'int' object is not callable
print(print_me)

Python:Why i can't put in e.g. (x+1) inside def function(here)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
def function(x+4):
return (x*5)+3
SyntaxError: invalid syntax
Why is that? i don't get the logic behind it?
(i know that it is a simple question but i couldn't find an answer to it.
i got many unclear feedbacks about my question,i am sorry about that as english is not my first language i couldn't clarify my question in short:
While i was learning about what return does,i compromised the logic with the logic behind f(x)= codomain as def function(x) = return codomain
but as you know there are functions in maths like f(x+2) = 5x+3. I thought maybe i could do the same on def function,the problem was it was giving a syntax error ,and i was curious about the design idea behind it and if there was an alternative solution to implement this in code.Thanks for answers!
This is a rather uncommon syntax you are suggesting and it is simply not part of Python's syntax, or any language that I know.
Although, what you want to do boils down to updating an argument before it is passed to a function. This can be done with a decorator.
def add_four_to_argument(f):
return lambda x: f(x + 4)
#add_four_to_argument
def func(x):
return (x*5)+3
print(func(1)) # 28
Since you asked about the logic behind that design decision, how would you expect that syntax to behave? Note that it is not the same x being shared everywhere. The following are the same:
x = 5
def f(x):
return x
print(f(x)) # Prints 5
x = 5
def f(y):
return y
print(f(x)) # Prints 5
This allows you to scope your variables and not have to debug the confusing shared state that exclusive use of global variables can cause.
Back to your question though, nowhere else in Python does the addition operator cause assignment (barring abuse of dunder methods...don't do that). So, in your hypothetical f(x+4), what happens to the x+4? Is that supposed to be a placeholder for x=x+4, or is that supposed to be assigned to some new variable representing some ethereal notion of "argument 1" for the function?
Let's dive into it a little further. What about the following example:
def f(x+y):
return x
Suddenly things got a whole lot more ambiguous. How do we call this function? Do we use f(1,2)? Do we only pass a single argument f(3) and guess which portion corresponds to x and which corresponds to y? Even if we figure out the semantics for calling such a function, what does the statement x+y actually do? Does it stand for x=x+y? For y=x+y? For arg1=x+y?
By not allowing the behavior you're describing, Python makes your intent clear. To do a thing, you have to tell Python to actually do that thing. From the Zen of Python,
explicit is better than implicit.

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

I'm struggling on functions, for example def a (...). What goes in the brackets? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new to programming and I recently started Python (3) in school. I'm having trouble understanding Functions. I've read some examples of Functions for example: def a (...). I understand "def" defines the functions and a names it, but I don't know what goes in the brackets, I heard its a parameter but when I Google it this comes up:
What is a parameter in computing? In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments.
Arguments, parameters, or input variables go within the brackets. These can be thought of as pieces of data you pass to a function for use within that function.
For (a very basic) example:
def plus_five(num):
my_other_number = num + 5
return my_other_number
This function would take in a number and within the scope of this function that number would be known as num. A new variable that exists only within this function would be created, named my_other_number, initialized to the value of the input num plus 5.
The function then returns the value of my_other_number.
In usage it would look something like:
my_num = 10
returned_number = plus_five(my_num)
print(my_num)
>>> 10
print(returned_number)
>>> 15
Think of an argument like you would the variable of a mathematical function:
f(x) = x+5
f(10) = 10+5 = 15
Try with an actual simple example:
def sumthis(a,b):
return a+b
c = sumthis(3,2)
print(c)
, a function to sum two numbers. The result (print sends stuff to the console so you are able to see) is: 5
Functions in programming are similar to functions in algebra. In algebra, functions are represented as f(x). In programming, they are defined using code but the representation is the same.
So if f(x) = x^2 in algebra, in python, it will be:
def f(x):
return x**2
And in algebra, f(2) = 4. So, in python f(2) will return 4.
What will probably help you the most is using google in a slightly different manner. Try searching for definitions of things such as:
define: programming argument
define: programming parameter
these two examples, when searched on google, each line separately, will show you a definition of each term. Any time you use "define" first on google, it will search a definition of the term or phrase that follows.

What is a DEF function for Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to coding Python and I just can't seem to understand what a Def function is! I have looked and read many tutorials on it and I still don't quite understand. Can somebody explain to me what it is, what I use it for, and give me some examples. For the examples please make them easy and understandable for a newb to Python. Thanks!
def isn't a function, it defines a function, and is one of the basic keywords in Python.
For example:
def square(number):
return number * number
print square(3)
Will display:
9
In the above code we can break it down as:
def - Tells python we are declaring a function
square - The name of our function
( - The beginning of our arguments for the function
number - The list of arguments (in this case just one)
) - The end of the list of arguments
: - A token to say the body of the function starts now
The following newline and indent then declare the intentation level for the rest of the function.
It is just as valid (although uncommon) to see:
def square(number): return number * number
In this case, as there is no indentation the entirety of the function (which in this case is just one line) runs until the end of line. This is uncommon in practise and not considered a good coding style.

Categories