Pages

Using len(sys.argv) directly to define a for loop in Python

dimanche 21 août 2016

Is there a way to use sys.argv directly to define the upper limit of a for loop in Python? I hope I'm using the right terminology. I don't have a lot of experience writing code, and I'm very new to Python. What I mean is that when I try this:

Code:

for x in range(1,len(sys.argv):
I get:

Code:

File "./dice.py", line 13
    for x in range(1,(len(sys.argv)):
                                    ^
SyntaxError: invalid syntax
So, I have to do this as a workaround:

Code:

y = len(sys.argv)

for x in range(1,y):
I was able to do the equivalent of what I want in Perl:

Code:

for $x (1..$#ARGV) {
I'm guessing I just have my parentheses wrong or need to put a space somewhere or something like that, but it's been a difficult question to google. I'd greatly appreciate any advice.

Let's block ads! (Why?)



Using len(sys.argv) directly to define a for loop in Python

Aucun commentaire:

Enregistrer un commentaire