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.
Answers:
Invoke that function
def calculation(a, b):
print(a)
return a
print(calculation(1, 3))
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
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))
.
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.
Invoke that function
def calculation(a, b):
print(a)
return a
print(calculation(1, 3))
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
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))
.