Python Command Line Arguments tutorial for Beginners

preview_player
Показать описание

In this Python command line arguments tutorial for beginners, you will learn how to pass arguments to your Python programs from the command line in the terminal window and how to use those argument values within your Python programs.

⭐ Become a full-stack dev with Zero To Mastery Courses:

👇 Follow Me On Social Media:

Python Command Line Arguments tutorial for Beginners

(00:00) Intro
(00:05) Welcome
(00:14) Getting Started
(00:26) argparse module
(01:25) Example 1
(04:22) Passing an argument at the command line
(05:57) Example 2
(11:30) Personalizing Rock Paper Scissors
(12:09) Quick f-String update
(13:48) Applying command line arguments to RPS
(15:29) Refactoring to personalize output
(19:45) Playing the game

📚 Tutorial References:

Was this Python Command Line Arguments tutorial for beginners helpful? If so, please share. Let me know your thoughts in the comments.

#python #command #arguments
Рекомендации по теме
Комментарии
Автор

I have to admit that I felt a little sceptical when someone recommended I use the argparse module when all I was looking for was if Python exposes arguments like in a list or something. But 5 minutes later, after skimming the documentation, boy, was I grateful that whoever it was that suggested argparse had done so! You’re definitely right to be introducing it this early in your Python series; it’s a great example of why your FIRST instinct when looking to solve a problem in Python should be to google for a module.

As always a great video, thanks!

PeranMe
Автор

Shouldn't we remove the 'required=True' from the add_argumetn( ) ?

but this way we can run from anywhere not just from the terminal

And, why do I need to mark the name parameter as 'nonlocal' if I'm not going to modify it?

Thank you so much, Dave for this amazing tutorial !!

alhabib_o
Автор

Thanks for the shoutout Dave! I really appreciate that.

Now, we can go further and localize this game by expanding the technique used in the hello_person example,

msg = {
"English": {
"player choice": "\n{name}, please enter... \n1 for Rock, \n2 for Paper, or \n3 for Scissors:\n\n",
"choice confirm": "{name}, please enter 1, 2, or 3",
},
"German": {
"player choice": "\n{name}, bitte geben Sie ein... \n1 für Stein, \n2 für Papier oder \n3 für Schere:\n\n",
"choice confirm": "{name}, bitte geben Sie 1, 2 oder 3 ein.",
}
}

lang = "German"
data = {"name": "Ahmad"}

print(msg[lang]["player choice"].format(**data))

# or maybe use
# print(msg[lang]["player choice"].format(name = 'Ahmad'))

Maybe it's better to save language dictionary to a separate module per language and then import necessary one into the main game file, this will allow contributors to add more languages (and will keep the game small, clean and easier to update independently)

Google translate was used for German messages, so please forgive me for any mistakes😁

ahmad-murery
Автор

Thank you for your videos. I am recently dealing with this error:
File "<stdin>", line 1
py rps8.py -n "Dave"
^^^^
SyntaxError: invalid syntax
Can you help me in fixing it? Thank you in advance

markyuuki
Автор

For some reason I keep getting : can't open file error message, I've tried everything even copying your lesson running it directly I save it in the LESSON 15 directory. It seems when I have to use commands in the terminal is when I run into problems. One thing I notice is that you always have a $ that comes before the py but mine doesn't the $. Everything else has been great, It must be something changed with Python since you did these lessons.

deepatterson
Автор

Does "description" refer to the entire function?

johnaweiss
Автор

My code works fine without the ".title" in line 33 and 34. Why do you include that?

quarkorion