Been working through learn python 3 the hard way and got to this. I understand (i think!) how it works but i'm not sure "why" i would use this. Can anybody give me a few ideas? Thanks so much in advance
Asked
Active
Viewed 90 times
-2
-
1Do you want to know what commandline arguments were passed to your program? That's what's in argv. Incidentally, you're usually better off using the higher level command line argument parsing packages available - there's two built in and a number of third party ones. – Marcin Jun 04 '20 at 15:47
-
Marcin and vikas thanks for your info. I've read the answer you suggested vikas but maybe i'm just stupid but I still don't quite understand why I would write a program using this. Wouldn't the person who runs it need to know what to input first? Or would that be known beforehand? – Dave1701 Jun 04 '20 at 16:36
1 Answers
0
You use argv to capture command-line arguments. Ler's say you have a program called fact.py that calculates and prints the factorial of an integer. You could call it as fact.py 70
and use argv to get the number.
from math import factorial
from sys import argv
print(factorial(int(argv[1])))

rbanffy
- 2,319
- 1
- 16
- 27