Click to See Complete Forum and Search --> : this is getting ridiculous. a Python problem...


inkedmn
12-06-2001, 06:14 PM
this just FEELS like it's ready to work, but i'm missing something totally obvious...


import sys
import string

if len(sys.argv) < 2:
print "No command line arguments to list"
else:
sys.argv.remove(sys.argv[0])
while len(sys.argv) >= 1 :
if sys.argv[i] == 'M':
if sys.argv[i+1].lower() == 'foo':
print sys.argv[i+1], "<---MATCH"
else:
print sys.argv[i], "UNCHECKED"



[edit]
the output should look something like this:

brett UNCHECKED
M
foo <---MATCH
M
linux UNCHECKED

(depending on the args passed at the CL)

[ 06 December 2001: Message edited by: inkedmn ]

EyesWideOpen
12-06-2001, 06:53 PM
Originally posted by inkedmn:
<STRONG>if sys.argv[i] == 'M': </STRONG>

What's in the variable 'i' at this point? Am I missing something with my Perl-glazed eyes?!

Gnu/Vince
12-06-2001, 06:57 PM
in sys.arg[i], i has no value. Of course it will crash. You need to initialize it.

inkedmn
12-06-2001, 07:24 PM
ok, i tried to fix it...

the last line is wrong, i'm almost positive, but i'm just trying to figure out how to:
- go back to the top of the loop
- process the next argument...


import sys
import string

if len(sys.argv) &lt; 2:
print "No command line arguments to list"
else:
sys.argv.remove(sys.argv[0])
for i in range (1, len(sys.argv)):
while len(sys.argv) &gt;= 1 :
if sys.argv[i] == 'M':
if sys.argv[i+1].lower() == 'foo':
print sys.argv[i+1], "&lt;---MATCH"
if sys.argv[i] != 'M':
print sys.argv[i], "UNCHECKED"
sys.argv[i+1]


i'm trying, BELIEVE me... :D

Gnu/Vince
12-07-2001, 12:50 AM
Correct me if I'm wrong, but your problem does not lie within your understanding Python: you don't know how to get a proper solution to the problem. So here's my advice: when you think of a problem, go get a piece of paper and a pen and write what you think on it. Then double-check it (post it if you want to) and make sure that there are no bugs in your algorithm. Then you can fire your editor.

jcrowe
12-08-2001, 01:30 AM
Do you need to use sys.argv[i + 1] ? shouldn't the for i in range automagicly increment?

Aragorn
12-08-2001, 06:44 AM
Originally posted by jcrowe:
<STRONG>Do you need to use sys.argv[i + 1] ? shouldn't the for i in range automagicly increment?</STRONG>

My sentiments exactly....

Aragorn

EyesWideOpen
12-08-2001, 07:28 AM
inkedmn, aren't you removing the first argument in the list, represented by argv[0], with this line:

sys.argv.remove(sys.argv[0])

And to clarify, the only way you want to print '&lt;--MATCH' is if "M foo" is given as an argument correct? Everything else given will print 'UNCHECKED' and you have to provide at least two arguments on the command line. Is that right?

jcrowe
12-08-2001, 11:33 AM
Originally posted by EyesWideOpen:
<STRONG>inkedmn, aren't you removing the first argument in the list, represented by argv[0], with this line:

sys.argv.remove(sys.argv[0])

</STRONG>

sys.argv[0] is the name(or file location) of the program. I don't think he wants that included in the list to match

jcrowe

jcrowe
12-08-2001, 01:00 PM
Does this do what you are trying to do?

#!/usr/bin/python2.2

import sys
import string

if len(sys.argv) &lt; 2:
print "No command line arguments to list"
else:
sys.argv.remove(sys.argv[0])
for i in range (1, len(sys.argv)):
if sys.argv[i] == 'foo':
if sys.argv[i-1] == "M":
continue
if sys.argv[i] == "M":
print sys.argv[i]
if sys.argv[i+1] == "foo":
print sys.argv[i+1], "&lt;---MATCH"
if sys.argv[i] != "M":
print sys.argv[i], "UNCHECKED"


The only thing that this needs is to check the foo variable against a lowercase version of foo. for some reason useing .lower doesn't work for me.

jcrowe

EyesWideOpen
12-08-2001, 06:34 PM
Originally posted by jcrowe:
is the name(or file location) of the program.

Ahh. It's different in Perl that's why I got confused.