Issue
I have a Python code as follows, I wrote a function as the same name as the Program name and gave an entry point for the function as follows:
import math
def Problem1():
total=0
for i in range(1000):
if i%3 == 0 or i%5==0 or i%6==0 or i%9==0:
total=total+i
return total
Now I went to terminal ran IPython, and then executed the following statements:
import Problem1
Problem1.Problem1()
It printed the output, even though I dont have a print statement, I have given a return statement, so what is happening here?
Another question : How do I run this directly from the command line, ie how to give something equivalent to void main() of C in Python?
Solution
What IPython does is it prints the output if you simply call a function or a variable
so if you just enter a variable name and hit enter it will print it. the same thing is happening for a function output. This is standard behavior in an interactive shell.
The idea behind this is that if you do interactive work you usually want to see your output directly.
To run this you would put your code in a *.py file where you actually call the function at the bottom of your file. so you don't need to do void main(). You then execute the code with the Interperter, i.e.
python yourfile.py
however there is a remotely similar pattern to the void main() thingy
Note that because Python is an interpreted language, you can simply put
print "hello world"
in a *.py file call it with the python executable and it right away works. There is no "main()" function that you need to define
Answered By - Retozi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.