0

Novice here. I'm extremely new to python programming and I don't have any coding experience. I'm doing this for one of my college requirements. As the title shows, I need help passing two string arguments to my python code. This is what I have so far:

import sys 
print("--------------------")
print("Initials:   OC")
print("Nickname:   Waldo ")
print("--------------------")
print("Initials:", sys.argv[2])
python3 
main.py arg1 arg2

This is what it's asking me to do: Question

Thank you for your help!

  • 1
    Does this answer your question: https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments – Hunter Tran Apr 24 '22 at 02:41

1 Answers1

0

You can use sys module:

myfile.py

import sys

print(sys.argv)

This prints you all the inputs with also the name of the program as the first item.

For example if you run python myfile.py a1 a2 a3 it will print ['ttt.py', 'a1', 'a2', 'a3'].

So if you want two inputs you have to run this:

myfile.py

import sys

print(sys.argv)

print("--------------------")
print(f"Initials:   {sys.argv[1]}")
print(f"Nickname:   {sys.argv[2]} ")
print("--------------------")

So if you run python myfile.py a1 a2 it will print this:

--------------------
Initials:   a1
Nickname:   a2
--------------------

If you have any question feel free to ask.

  • I need to use this specific command: python3 main.py arg1 arg2. How can I incorporate this into my python? – Osvaldo Carrillo Apr 24 '22 at 06:25
  • @OsvaldoCarrillo This is exactly what I explained above. If you put the code I said in my answer in `main.py` file and then type `python3 main.py arg1 arg2` then the output would be `arg1` for *Initials* and `arg2` for *Nickname*. Like you wanted. – Hadi Hajihosseini Apr 25 '22 at 00:53