Posts: 1,358
Threads: 2
Joined: May 2019
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.
Posts: 6,799
Threads: 20
Joined: Feb 2020
(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
Posts: 230
Threads: 39
Joined: Mar 2020
thank you both,
i'm delving deeper in my studies...
Posts: 230
Threads: 39
Joined: Mar 2020
Nov-21-2022, 08:01 PM
(This post was last modified: Nov-21-2022, 08:01 PM by astral_travel.)
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 ?
Posts: 453
Threads: 16
Joined: Jun 2022
Nov-21-2022, 08:13 PM
(This post was last modified: Nov-21-2022, 08:13 PM by rob101.)
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
Posts: 230
Threads: 39
Joined: Mar 2020
Nov-21-2022, 08:29 PM
(This post was last modified: Nov-21-2022, 08:30 PM by astral_travel.)
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...
Posts: 453
Threads: 16
Joined: Jun 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.
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
Posts: 230
Threads: 39
Joined: Mar 2020
Nov-21-2022, 08:55 PM
(This post was last modified: Nov-21-2022, 08:55 PM by astral_travel.)
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")
Posts: 453
Threads: 16
Joined: Jun 2022
Nov-21-2022, 09:22 PM
(This post was last modified: Nov-21-2022, 09:22 PM by rob101.)
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
Posts: 230
Threads: 39
Joined: Mar 2020
i have no idea where to start, can you refer me to a material i can read in order to accomplish this task ?
|