Aztlan
Posts: 4
Joined: Fri Jul 20, 2012 7:40 pm

Python 2.7.3

Fri Jul 27, 2012 7:49 pm

can anyone help l can not get is code to work on python 2.7.3?

Code: Select all

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


thank you

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: Python 2.7.3

Fri Jul 27, 2012 10:54 pm

When asking for help for this sort of thing, it's really helpful if you can include
the error you are getting or in what way it is not working as expected, and for this
example what command line you are using to run it.

khh
Posts: 49
Joined: Thu Jul 26, 2012 12:16 am

Re: Python 2.7.3

Sat Jul 28, 2012 3:19 am

In all likelihood assignment fails. This is because the length of argv is dependant on how many arguments are defined at the command line, and touple assignment (that's the "script, first, second, third = argv" line) requires that you have an equal number of items on both sides of the '='. Thus your script would only work if argv contained exactly four arguments.

Try using loops instead

Code: Select all

from sys import argv
print "The script is called:", argv[0]
i = 1
while i < len(argv):
    print "Variable:", argv[i]
    i += 1
And so on and so forth.

kobzar
Posts: 37
Joined: Tue Jul 17, 2012 1:09 pm
Location: Ukraine

Re: Python 2.7.3

Mon Jul 30, 2012 6:28 am

khh wrote:In all likelihood assignment fails. This is because the length of argv is dependant on how many arguments are defined at the command line, and touple assignment (that's the "script, first, second, third = argv" line) requires that you have an equal number of items on both sides of the '='. Thus your script would only work if argv contained exactly four arguments.

Try using loops instead

Code: Select all

from sys import argv
print "The script is called:", argv[0]
i = 1
while i < len(argv):
    print "Variable:", argv[i]
    i += 1
And so on and so forth.
Perfect !!!
No Fate!

Return to “Python”