Reputation: 53
This is my code:
def isPrime(n):
if n>1:
for i in range(2, n):
if (n%i)== 0:
print(n, "is not a prime number")
print(i, "*", n//i, "=", n)
break
else:
print(n, "is a prime number")
else:
print(n, "is not a prime number")
Earlier I have used idle and Jupyter notebook but now I am trying to run it via command line and I don't know how to enter argument for the function defined. I'm using cmd for the first time for running scripts. This is what it shows:
Upvotes: 4
Views: 21110
Reputation: 31
Curiously, using cmd.exe on my computer, the -c function only works if you use double quotes on the outside (ie the example above may not have worked because it used single quotes):
python -c 'print("It worked!")'
shows no output, while
python -c "print('It worked!')"
Prints 'It worked!' as expected. I presume this is particular to windows cmd.exe
Upvotes: 3
Reputation: 11
Actually the -c solutions above (such as python -c 'isPrime(11)') did not work for me, as they leave a blank line. I can call a function from a Python environment itself, so first I locate, then run:
C://Desktop/MyPythonFiles/> python
>>> import myfunction; myfunction.isPrime(13)
The only difference with Leonardo's answer is that you may write the call on the same line using a semicolon.
Upvotes: 0
Reputation: 1080
to do this you have to open the cmd
then first start python interpreter:
C:\Users\YourUser> py
or
C:\Users\YourUser> python
(it depends form your python version)
then will appear this:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
NOTE:
this message can be different because of your python version or your computer >system
now you only have to import your file as a module and then call your function like this:
>>> import yourfilename
>>> yourfilename.foo(args_to_be_passed_to_your_function)
NOTE:
to fo this your file has to be in your cwd (current work directory) or in >your python path
if instead you want to execute your file you have to do:
>>> path/to/your/filename.py
to quit the python interpreter write:
>>> exit()
Upvotes: 1
Reputation: 152
You can use the flag -c while doing python like this:
python -c 'isPrime(11)'
you can also import this way as well
python -c 'import foo; foo.isPrime(2)'
or do this
python -c 'from foo import isPrime; foo.isPrime(n)'
Upvotes: 5