Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first function
#11
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.
Reply
#12
(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
Reply
#13
thank you both,
i'm delving deeper in my studies...
Reply
#14
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 ?
Reply
#15
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#16
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...
Reply
#17
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#18
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")
Reply
#19
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")
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#20
i have no idea where to start, can you refer me to a material i can read in order to accomplish this task ?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020