Need more than 1 value to unpack?

Started by
20 comments, last by Lactose 9 years, 9 months ago

You are confused about the line "test11.py first 2nd 3rd" Your code should match exactly to what he lists in the text:


from sys import argv



script, first, second, third = argv



print "The script is called:", script

print "Your first variable is:", first

print "Your second variable is:", second

print "Your third variable is:", third

Notice that you have an extra line that you should remove?

What you may not understand is what a program is and how they are executed? In mouse environments, you don't realize what is happening when you click a button to start up a program it just happens. But in the old days and for people who enjoy text interaction (very powerful once you learn to use it, look into linux and unix environments for more examples), many programs are started by typing their name! Where do you type their name? Well, in a terminal in linux or the command prompt in windows. Then, you can add different parameters and mix programs to do very interesting things. I haven't used windows for a long time, but I believe the following directions will get you on the right track.

1. Remove the line you shouldn't have in your Python script and save.

2. Open a command prompt. Try the start button and typing in cmd or Google the answer.

3. Navigate to where you want to run the program you wrote. Try typing the following: cd "E:\f\mohamed\New folder (5)"

I got that directory from your screenshot by the way.

4. Execute your program, but with extra parameters (these allow the program to adapt to do different things). Type the following: python test11.py first 2nd 3rd

5. Your program should output this:


The script is called: test11.py

Your first variable is: first

Your second variable is: 2nd

Your third variable is: 3rd

Interesting tidbit: You are actually running the program called "python" and "test11.py" tells the program to run the code you wrote! Python then passes the rest of the arguments (first, 2nd, 3rd) to the code you wrote.

When you use and IDE to run stuff it runs the python program in the background and outputs the results to you.

Advertisement

Thanks for helping me & smile.png ?Sry about Difficulty understanding

now what ?! l2wx.png

You first need to type cd /D "E:\f\mohamed\New folder (5)\" to change to the correct folder.

The /D let's the 'cd' command know that it has permission to switch drives to the 'E:' drive.

You'll know if it works, because the C:\Users\________ > at the front of each line would turn into the path you typed in.

Then, since you are at the current folder of your .py script, you can execute your script by typing: python test11.py

However, you want to also pass in parameters, so instead of just typing "python test11.py", type python test11.py aarvark purple mongoose instead. The last three words can be anything you want (because they'll be passed to your script, and your script will have to handle them), as long as there is just a single space between each one and as long as you keep to letters and numbers, and avoid fancy symbols (some symbols have special meanings).

:)

thanks bro

now what ?!hxvq.png

Type: python test11.py aarvark purple mongoose

The last three words can be anything you want (because they'll be passed to your script, and your script will have to handle them), as long as there is just a single space between each one and as long as you keep to letters and numbers, and avoid fancy symbols (some symbols have special meanings).

Where I should type python test11.py aarvark purple mongoose

i already typed it !?

The black window with the white text is called the 'DOS Prompt' the 'DOS Window' or the 'Command Prompt' or 'Command Window'. You use it alot in programming.

In the Command Prompt window (the black window with the white text), make sure you are executing within the correct folder. You can see what folder you are 'in' (i.e executing within), because the command prompt tells you what folder it is running inside.

It shows it right here, where the > arrow is. I highlighted the text in yellow here: (normally it is just white text)

screenshot_1.png
The command prompt executes (i.e. 'runs') programs. You type in the name of the program, followed by a space, followed by zero or more 'arguments'.

CD is one such program. It stands for 'change directory'. 'directory' is another word for 'folder'. The 'cd' program changes what folder the command prompt is currently executing within. This is called the 'current working directory'.

To use CD, you must type in its name, followed by the arguments you want to give it. In your current situation, you want to use CD to change your directory to the directory that your python script is in.

So, in the command prompt, type: cd /D "E:\f\mohamed\New folder (5)\"

That'll execute the 'cd' program, and give it the /D argument and the folderpath argument you want to move to.

You'll know if it worked, because it'd change the 'current working directory' to the path you specified.

screenshot_2.png

Once your current working directory is the same directory (i.e. 'folder') that has your python script in it, you can tell the 'python' program to run your .py script.

Do this by typing, in the command prompt, the name of the python program (called 'python') and passing it several arguments. The first argument would be the name of your script. The other arguments will be passed into your script so your script can handle it.

The command you type is:

python test11.py argument1 argTwo arg3

You type it into the command prompt, just like you do with the 'CD' program.

screenshot_3.png

c0bn.png

Now what !?

You need to delete the "python test11.py etc.." line from your script, since it doesn't belong there.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Thanks all

Good now in cmd w62g.png

but i can't make From Notpad++ ,,from Cmd only

right ?abxr.png

Thanks all :)

This topic is closed to new replies.

Advertisement