4

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

4
  • When you are in cmd, from prime import isPrime. Commented Jan 31, 2021 at 10:09
  • You can run files from the cmd, not functions Commented Jan 31, 2021 at 10:09
  • Does this answer your question? how to run python files in windows command prompt? Commented Jan 31, 2021 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 Commented Jan 31, 2021 at 10:13

4 Answers 4

5

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)'
Sign up to request clarification or add additional context in comments.

1 Comment

For the last one: python3 -c 'from foo import isPrime; isPrime(...)'
3

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

1 Comment

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.
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()

Comments

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

1 Comment

Welcome to Stack Overflow. Please don't post screenshots of text. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.