Python Forum
first function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: first function (/thread-38692.html)

Pages: 1 2 3 4


RE: first function - jefsummers - Nov-14-2022

Defining a function does not execute it.
Defining a function twice wastes CPU cycles and again does not execute it.
The point of a function is to write things once and then execute multiple times. It does not require re-definition each time you run it.


RE: first function - deanhystad - Nov-15-2022

(Nov-13-2022, 08:37 PM)ndc85430 Wrote: You don't need to define the function every time in the loop, at least not if you pass arguments to it rather than making it a closure as you have done.
For it to be a closure you need to define a function inside the scope of another function. Calling the outer function generates a context/closure within which the nested function executes. In astral travel's example the function is defined inside a loop in the global scope. There is no outer function, thus no function context, thus no closure.

This can be seen in the example below:
functions = []

for i in range(5):
    def x():
        return i

    functions.append(x)

for y in functions:
    print(y())
Output:
4 4 4 4 4
All the functions return the final value of "i". If this were a closure, the value of "i" in the closure would be the current value of "i" when the function was added to the list as shown in this example that creates a closure for the nested function.
functions = []

def outer(i):
    def x():
        return i
    return x

for i in range(5):
    functions.append(outer(i))

for y in functions:
    print(y())
Output:
0 1 2 3 4



RE: first function - astral_travel - Nov-15-2022

thank you both,
i'm delving deeper in my studies...


RE: first function - astral_travel - Nov-21-2022

okay, so here's my first function (even got to pass the arguments from a user input),

def multiply(a, b):
    return a*b


user_input = int(input("enter the first number: "))
user_input2 = int(input("enter the second number: "))

print(multiply(user_input, user_input2))
suppose i want to call the function eternally, like, keep asking for the user input after each printing of the result....how is it possible to do that ?


RE: first function - rob101 - Nov-21-2022

Just say while True:, which will always be so...

while True:
    user_input = int(input("enter the first number: "))
    user_input2 = int(input("enter the second number: "))

    print(multiply(user_input, user_input2))
What you should do, is to investigate how to catch an error in the user input, so that the script does not crash.


RE: first function - astral_travel - Nov-21-2022

yep that does the work,
i actually tried something similar but didn't put the while True in the right place..

alright thanks man
-----------

okay, i'll try to find something to read about exceptions/stuff like that...


RE: first function - rob101 - Nov-21-2022

No worries.

You need look no further than this Forum; just read back through the posts here and you'll soon see what you need.

Keep up with the learning; you're doing great.


RE: first function - astral_travel - Nov-21-2022

thanks !

is this code better ? it seems to do the job

def multiply(a, b):
    return a*b


count = 0

while count < 100:
    try:
        user_input = int(input("enter the first number: "))
        user_input2 = int(input("enter the second number: "))

        print(multiply(user_input, user_input2))

        count += 1

    except:
        print("enter only integer numbers")



RE: first function - rob101 - Nov-21-2022

It does the job, but it catches every possible exception, which is fine for a basic 20 line script; the issue is that it's only good for this script.

What I had in mind is for you to code a function (let's call it get_int():) that could be used whenever you need to get an integer value from a user input, regardless of what the rest of the script does. You can then save the function and simply use in whatever script you need it for. It could even be imported.

If you look at the exception that Python throws, you'll see ValueError, which is the only one that you need to catch.

except ValueError:
        print("enter only integer numbers")



RE: first function - astral_travel - Nov-21-2022

i have no idea where to start, can you refer me to a material i can read in order to accomplish this task ?