3

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:

Command Prompt image

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user12856241
  • 43
  • 1
  • 1
  • 3
  • When you are in cmd, from prime import isPrime. – lllrnr101 Jan 31 '21 at 10:09
  • You can run ***files*** from the cmd, not ***functions*** – Tomerikoo Jan 31 '21 at 10:09
  • Does this answer your question? [how to run python files in windows command prompt?](https://stackoverflow.com/questions/39462632/how-to-run-python-files-in-windows-command-prompt) – Tomerikoo Jan 31 '21 at 10:09
  • You have a few mixed problems here. In the command line, you entered an interactive Python interpreter (by doing `python`). Inside a Python interpreter, you can import your `py` file by `import prime` and then run the function by calling it: `prime.isPrime(10)`. If you want to run the file from the command line, you need to do `python prime.py` but you have to have some runnable code. Right now you just define a function. You need to also call it inside the file, probably under a main guard/function – Tomerikoo Jan 31 '21 at 10:13

4 Answers4

3

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)'
YamiAtem
  • 132
  • 1
  • 9
2

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

Mirdrop
  • 21
  • 1
  • This was very helpful as I couldn't understand what was wrong. Not only did I not get a `print()` output when using single quotes, but I also got a SyntaxError when I tried to do an `import`. Using double quotes solved it, so thanks. – jshd Nov 23 '22 at 14:05
1

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()
Leonardo Scotti
  • 1,069
  • 8
  • 21
0

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.

see screenshot

  • 5
    Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your answer. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Apr 23 '22 at 15:47