While loop with sys.argv

Reply

Join Date: Aug 2009
Posts: 3
Reputation: huskeraider is an unknown quantity at this point 
Solved Threads: 0
huskeraider huskeraider is offline Offline
Newbie Poster

While loop with sys.argv

 
0
  #1
Sep 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1,018
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 167
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
Veteran Poster

Re: While loop with sys.argv

 
0
  #2
Sep 14th, 2009
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:
  1. import sys
  2.  
  3. #-4 means start from the back and count back 4
  4. #and then the rest of the arguments (:
  5. for arg in sys.argv[-4:]:
  6. print arg
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: huskeraider is an unknown quantity at this point 
Solved Threads: 0
huskeraider huskeraider is offline Offline
Newbie Poster

Re: While loop with sys.argv

 
0
  #3
Sep 15th, 2009
Originally Posted by paulthom12345 View Post
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:
  1. import sys
  2.  
  3. #-4 means start from the back and count back 4
  4. #and then the rest of the arguments (:
  5. for arg in sys.argv[-4:]:
  6. print arg
Hope that helps
i would prefer to the example you provided but i think part of the deal is me learning while loops. I've seen several examples of while loops but couldn't make it work with the above criteria.

Thanks,
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 106
Reputation: zachabesh is an unknown quantity at this point 
Solved Threads: 16
zachabesh's Avatar
zachabesh zachabesh is offline Offline
Junior Poster

Re: While loop with sys.argv

 
0
  #4
Sep 15th, 2009
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.

  1. n = 3
  2. while n <= 6:
  3. print lst[n]
  4. n += 1
You could also use a for loop:
  1. for x in range(3,7):
  2. print lst[x]
-Zac
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: huskeraider is an unknown quantity at this point 
Solved Threads: 0
huskeraider huskeraider is offline Offline
Newbie Poster

Re: While loop with sys.argv

 
0
  #5
Sep 15th, 2009
So, i would replace "n" the 3rd argument?

thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1,018
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 167
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
Veteran Poster

Re: While loop with sys.argv

 
0
  #6
Sep 15th, 2009
Actaully if you wanted the third item till the sixth item, the above code would almost be correct. Apart from one thing:

  1. #n = 3
  2. #if n = 3, then the fourth item will be printed
  3. n=2
  4.  
  5. while n <= 6:
  6. print lst[n]
  7. 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
Reply With Quote Quick reply to this message  
Reply

Tags
python, script

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1701 | Replies: 5
Thread Tools Search this Thread



Tag cloud for python, script
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC