Page 1 of 1

Python 2.7.3

Posted: Fri Jul 27, 2012 7:49 pm
by Aztlan
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

Re: Python 2.7.3

Posted: Fri Jul 27, 2012 10:54 pm
by timhoffman
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.

Re: Python 2.7.3

Posted: Sat Jul 28, 2012 3:19 am
by khh
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.

Re: Python 2.7.3

Posted: Mon Jul 30, 2012 6:28 am
by kobzar
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 !!!