How do I run a python file from command prompt: not getting any response

Question:

How can I run below file?

Here is my code:

main.py

def calculation(a, b):
   print(a)
   return a

I have tried, py main.py it does not return anything.

Asked By: user20256558

||

Answers:

Invoke that function

def calculation(a, b):
   print(a)
   return a

print(calculation(1, 3))
Answered By: mlokos

You can access command line arguments using sys.argv. This is a very minimal example.

my_prog.py

import sys

def calculation(a, b):
   print(a)
   return a

if __name__ =='__main__':
    a,b = sys.argv[1:]
    calculation(a,b)

To run:

host$ python3 my_prog.py 1 2
1
Answered By: bn_ln

You have to call the function to run it. Add calculation(a, b) to your code with a and b as the values to pass. For example:

def calculation(a, b):
   print(a)
   return a

calculation(1, 2)

To print the return value, you would do print(calculation(1,2)).

Answered By: Python Nerd
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.