Problem passing argument via Python command line

Asked

Viewed 121 times

1

I am a beginner in Python and am facing the following problem to pass an argument via Python command line.

from math import pi
import sys


def circulo(r):
    return pi * float(r) ** 2


if __name__ == '__main__':
    r = sys.argv[1]
    area = circulo(r)
    print('Area do circulo ', area)

I’m having the following exit at the terminal:

Saida do Pycharm

  • you have to put in place of 1 the value 0, since the first index is 0

  • 1

    I ran your code on my machine and had no problems. You are calling the file python by name alone? For right is right: $ python nome_arquivo.py args.

1 answer

2


Specifically in this case, the interpreter is understanding that the program is a shell script and not a program in Python. Add the shabang -- Be the #!/usr/bin/python or #!/usr/bin/env pythom -- at the beginning of the file for the program be correctly identified or call it through python argumentoLinComand.py.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.