This question already has answers here:
main() function doesn't run when running script [duplicate]
(5 answers)
Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?
(5 answers)
Closed 2 days ago.
I'm a newbie in python I'm trying to use def function and every time I run the code it gives me a blank output. I'm using pyroid3 in my phone since I dont have any laptop/desktop.
I'm trying to get thru with this code
def greet():
print('hi')
You have to call your function. With def you only declare it.
In python we calling function by adding ().
Like this:
def greet():
print('Hello')
greet()
I apologize if it is an obvious question but have you tried to call your function? For example try to add greet() at the end of your code. It will look like this:
def greet():
print('hi')
greet()
Feel free to ask if you need explaination!
it's possible that the output isn't displaying because you're not calling the function.
def greet():
print('hi')
# Call the function
greet()
Related
I'm newbie with python programming and I'm facing an issue with Windows Memory, so I want to understand if the way I'm calling the other "subprogram" ic causing this problem.
Basically I've done a main program from which I call the other .py (called second_py.py) like this:
from second_py import second_py
variable_1, variable_2 = second_py(input_1, input_2, input_3)
the second_py is ended with a return and the input_1/2/3 can be different from where this second_py.py is executed in the main program.
is this way correct?
I've seen that it is also possible to use the subprocess.call but honestly it is not clear to me how to pass to it the inputs.
Here is a working example:
second_py.py
def my_function(a,b,c):
print ("I am a function from 'second_py' file!")
return(a+b+c, a*b*c)
first_py.py
from second_py import my_function
#Call the second_py function here
v1,v2=my_function(3,5,4)
print(v1,v2)
Output:
This question already has answers here:
python print all function calls to know the script flow
(2 answers)
Closed 1 year ago.
What specific syntax needs to be added to the code below in order to print out the full trace of the chain of function calls that result in each time that four.myFunction() gets called in the code sameple below?
one.py
import two
import three
two.twoFunction()
three.threeFunction()
two.py
import four
def twoFunction():
four.myFunction()
three.py
import four
def threeFunction():
four.myFunction()
four.py
def myFunction():
print("trace of which functions called me, back to original.")
This is important because we have functions that seem to be run more times than the code looks like the functions should be run.
If we can isolate the chain of calling scripts/functions for each call to four.myFunction(), we will be able to more closely diagnose what might be going on.
We are using Python 3.
With each of your functions, you can require an input like def ...(past_calls):and add the current function to it when calling another.
i.e.
def NthFunction(past_calls):
# Call next function, adding current to the trace.
NextFunction(past_calls + [n])
And when you are printing your results, you can use the information given as the parameter (you can also add more details if need be).
This question already has answers here:
How to get just the return value from a function in Python?
(2 answers)
Closed 3 years ago.
I know this this questions been asked before, but I can't seem to get the answers to work. I'm trying to pass the variable things from one script to another.
test.py
def addstuff(word):
things = word + " good."
return things
test2.py
from test import addstuff
addstuff("random")
stuff = things + "morethings"
Ive also tried importing with
from test import *
things doesn't show up as defined in test2, How can I fix this?
The addstuff("random") does not store the output of your addstuff() function from test.py into any variable, its just discarded.
The things in your test2.py does not mean anything to the program until its not assigned to any variable.
Here's the correct way to do it:
from test import addstuff
things=addstuff("random")
stuff = things + "morethings"
We're assigning the output of addstuff("random") (ie "random good.") to things and then adding "morethings" to it.
This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 6 years ago.
I am learning a tutorial for Python language and trying to write a basic "Hello World!" program.
But when I do all steps described in the book I receive an error.
>> print "Hello World!"
SyntaxError: Missing parentheses in call to 'print'
Why I am getting this error?
Is my book wrong?
it seems that you're using Python 3.x.
In python 3.x, the print statement is a function and you need to use it as a function like this
print("Hello World!")
Your book is right, but might be outdated a bit. It seems it describes Python version 2, but you try to run your example on the version 3.
Python 3 has changed some features and this one is the most annoying to switch from P2 to P3.
"print" statement changed to function rather than operator as was in P2.
Calling function you should always use parentheses.
So, if you want to run your program in Python3 you should call it:
print("Hello World!")
And that's it.
If you want to use examples from your book as is - install Python2 and it should work.
This question already has answers here:
Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?
(5 answers)
Closed 6 years ago.
I have code with many functions and main, when I am trying to run the code it's not working and showing like it run successfully. When I am run the debugger it's show me that it run only on the functions' names. so I am pretty sure the problem it's with the main. how can i solve it?
main() is not run implicitly (like in C or Java). In Python you have to explicitly call if you want your code to run.
def main():
some_code()
if __name__ == "__main__":
main() # actually run main
Note that main does not have to be named main - it may be arbitrary named function. Moreover, code to run does not even have to be enclosed in any function. Consider file with content like that:
print "abc"
It'll simply print "abc" on standard output.