| | |
While loop with sys.argv
![]() |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
I'm just learning python. I'm trying to figure out while loops with s.argv in a larger script on my network. hopefully this small example will show basically what i'm trying to do.
argtest.py
import sys
for arg in sys.argv:
print arg
#which gives me:
Life
is
good
but
could
be
better
using a while loop i'm trying to print out the last 4 arguments:
"but could be better", while allowing any additional arguments i add to be printed (8,9,10,so on)
I'm trying to do this with a list of servers in my network but this a shorter example.
I'm sure most of you can do this in your sleep but i'm just learning and it's not so easy for me.
I would greatly appreciate any assistance.
thanks.
argtest.py
import sys
for arg in sys.argv:
print arg
#which gives me:
Life
is
good
but
could
be
better
using a while loop i'm trying to print out the last 4 arguments:
"but could be better", while allowing any additional arguments i add to be printed (8,9,10,so on)
I'm trying to do this with a list of servers in my network but this a shorter example.
I'm sure most of you can do this in your sleep but i'm just learning and it's not so easy for me.
I would greatly appreciate any assistance.
thanks.
So basically do you always want the last 4 arguments printed out? Because if that is the case i wouldn't use a while loop i would go something like this:
Hope that helps
Python Syntax (Toggle Plain Text)
import sys #-4 means start from the back and count back 4 #and then the rest of the arguments (: for arg in sys.argv[-4:]: print arg
Make it idiot-proof, and someone will just make a better idiot
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
So basically do you always want the last 4 arguments printed out? Because if that is the case i wouldn't use a while loop i would go something like this:
Hope that helpsPython Syntax (Toggle Plain Text)
import sys #-4 means start from the back and count back 4 #and then the rest of the arguments (: for arg in sys.argv[-4:]: print arg
Thanks,
From what you've said, it sounds like you have to print out elements 3 through 6 (if 0 is the first element) of a list. This will print those 4 elements, doesn't matter how long the list is.
You could also use a for loop:
python Syntax (Toggle Plain Text)
n = 3 while n <= 6: print lst[n] n += 1
python Syntax (Toggle Plain Text)
for x in range(3,7): print lst[x]
-Zac
Actaully if you wanted the third item till the sixth item, the above code would almost be correct. Apart from one thing:
This is because a list counts from 0, then 1,2,3 and so on. So when you go
Hope that helps
Python Syntax (Toggle Plain Text)
#n = 3 #if n = 3, then the fourth item will be printed n=2 while n <= 6: print lst[n] n += 1
This is because a list counts from 0, then 1,2,3 and so on. So when you go
list[2] you're actually asking for the 3rd element in the list.Hope that helps
Make it idiot-proof, and someone will just make a better idiot
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
![]() |
Similar Threads
- Python GUI Programming (Python)
- Starting Python (Python)
- K-means clustering (Python)
- What does the sys module do in python? (Python)
- Collision Detection (Python)
- Problem with Pyinstaller 1.3 and PyQt4 (Python)
- Infinite loop (Python)
- have any one ever implement John Conway's Game of Life (Python)
- Python 2.4: AttributeError: 'module' object has no attribute 'argv' (Python)
Other Threads in the Python Forum
- Previous Thread: return each element of a list
- Next Thread: Pygame - Get screen size?
Views: 1701 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for python, script
aes aliased array backdoor beginner c++ c/c++ class client code console contest convert corners countdown csv cx-freeze database design django dll engine error examples excel execution extensions fdlib file flash formatting framework freelance function gtk gui hide homework http iframe images input java language launcher library line linux list lists logging login loop math mp3 mysql mysqldb obexftp oop perl php port/bindings program programming projects py2exe pygame pyglet pygtk python random read remote return ruby script search server shebang simple socket sorting ssh syntax table tcp/ip text thread threading tkinter tooltip traceback tutorial ubuntu variable video web window wxpython zip







