In this lesson you give python some arguments when starting the code.
It is called command line arguments.
In contrast to the input variables that got its arguments while the code is running.
So you have to give it something for the
first,
second,
third variables.
example:
python3 ex13.py uno dos tres
As you can see it already got one value.
That is the first variable (script) wich is assigned to the name of the file.
I can only guess it pick the first variable for the name of the file.
This is interesting. So if anyone can give an explanation I am all ears.
@gpkesley can u explain briefly, I couldnât understand. I pasted the code in powershell and I gives SyntaxError. why is this python 3.x ex.xx everywhere?
The code you written in the file (ex13.py) says to import a python module called argv.
This module manages values that you add to the command line when you execute the file. Thereâs different types of arguments but youâll get to that later.
When you add these arguments to the command line as in the examples above, at runtime they need somewhere to go. The line written into the file that equals argv, is where the arguments get unpacked.
As mentioned above, the first argument relates to the ex13.py script, (hence the name but actually you can call it anything). The first, second, third variable labels in the book (or uno, dos, tres in @ulfen69 example or first_arg, second_arg, third_arg in my example) and where the command line arguments get unpacked to. This process is managed by argv, which is why you need to import it.
I expect the syntax error you got was my typo but capitalising âpython3â as âPython3â. Sorry about that!
Also @Dash_Ashutosh9876543 the need to keep writing python ex.xxx everywhere is you instructing your computer to run a python file.
You write the file and save it. But your computer needs to know what to do with it. You could have lots of different languages installed on your computer, like Ruby, JavaScript, etc so you need to be specific about what to run that file with. Remember the computer is dumb so you need to be explicit and tell it:
âUsing Python framework version 3, run this ex13.py fileâ.
Imagine you wanted to open that file in your text editor, you could do that from the command line too. Just replace âpythonâ with the editors name and the file (depends on which editor you use but ânotepad ex13.pyâ should work I think).
I used to do that by only typing python in cmd and thatâs it⌠Moreover cmd doesnât recognizes file nameâŚ
What about my previous problem?what should i do?
To me it looks like you are starting the python shell.
Does look like this:
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux (not Linux on your pc)
Type "help", "copyright", "credits" or "license" for more information.
>>>
@Dash_Ashutosh9876543 when you only use python on CMD you are opening the python interpreter which lets you interact with the python framework interactively. You can write small programmes here or hack about but itâs not a good idea to write an entire application like this (mostly because when you quit the session, everything in memory is disregarded).
Itâs a common paradigm to call a piece of software and provide some parameters for it as instructions, from the command line, as you probably know.
Hey @Dash_Ashutosh9876543, Iâm guessing you didnât watch the video for this exercise? Actually, you should probably go back and watch all the videos since theyâre required for reading this book and understanding it.
But, you just need to run your script correctly. You are starting cmd. You should instead start PowerShell (BUT NOT POWERSHELL ISE).
Then, when you start powershell you should see this:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\Zed>
I have âZed>â here but you might have âDash>â instead on that last line. Now stop, and very carefully read that last line.
Did you stop and read it? Good, at the end there is > then a space then a blinking cursor. Thatâs where you type your commands. The command that @gpkesley and @ulfen69 are telling you to run are NOT python code. They are PowerShell commands. You then type this after the > and hit ENTER:
python ex13.py apple orange grape
What this does is commands PowerShell to:
Run the python.exe program, which is what runs your python code.
Tell python.exe to load your ex13.py file.
Tell your ex13.py file to process three arguments: apple orange grape
ex13.py then puts those three words into your argv variable.
Now, this is mentioned in the exercise, so that means youâre also not reading the exercises carefully. You need to start reading them very carefully and when you have an error, go back and re-read them to see if you missed something. In fact, this exact problem should be called out in giant red warnings right before you run this command.
Lastly, you should go through the Appendix A in the book that teaches you the command line before you go any further. Not knowing the command line will hold you back in ALL programming so take the time to learn the basics.
Donât know about the font but It is termux app. I really was confused at first how to use it but when I came to know how to use it , itâs awesome . Im Learning python without a laptop once this quarantine ends going to buy once asap . Thanks @zedshaw ,you really got me into this ⌠Python is fun.