469,332 Members | 7,001 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

What is wrong with this use of sys.argv

Deathwing
Hi can any one tell what is wrong with this code, whenever I enter two arguments I still get my error message that i must enter at least two arguments.
Here is the code.

Expand|Select|Wrap|Line Numbers
  1. #command line arguments
  2.  
  3. import sys
  4.  
  5. if len(sys.argv)!=2:
  6.     sys.exit("You must enter at least 2 values.")
  7.  
  8. #arguments
  9.  
  10. a = int(sys.argv[1])
  11. b = int(sys.argv[2])
  12. sum = a + b
  13.  
  14. print sum
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
May 22 '07 #1
4 2092
bvdet
2,851 Expert Mod 2GB
Hi can any one tell what is wrong with this code, whenever I enter two arguments I still get my error message that i must enter at least two arguments.
Here is the code.

Expand|Select|Wrap|Line Numbers
  1. #command line arguments
  2.  
  3. import sys
  4.  
  5. if len(sys.argv)!=2:
  6.     sys.exit("You must enter at least 2 values.")
  7.  
  8. #arguments
  9.  
  10. a = int(sys.argv[1])
  11. b = int(sys.argv[2])
  12. sum = a + b
  13.  
  14. print sum
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
Try this:
Expand|Select|Wrap|Line Numbers
  1. if len(sys.argv)!=3:
  2.     ........
The first item in sys.argv is the name of the program. Therefore, there should be three arguments.
May 22 '07 #2
Try this:
Expand|Select|Wrap|Line Numbers
  1. if len(sys.argv)!=3:
  2.     ........
The first item in sys.argv is the name of the program. Therefore, there should be three arguments.
DOH!! how stupid of me of course thanks BV, you know I new that the name was the first argument i just didn't think I had to include it in my code how stupid of me.
May 22 '07 #3
bartonc
6,596 Expert 4TB
Hey, DW. Please try to use more descriptive titles in your queries, OK. (see new title)
This will help us help you and I won't have to go around renaming your thread.

Thanks.
May 22 '07 #4
bvdet
2,851 Expert Mod 2GB
DOH!! how stupid of me of course thanks BV, you know I new that the name was the first argument i just didn't think I had to include it in my code how stupid of me.
Hey - don't let it bother you! Sometimes the most obvious mistakes are tough to spot. You had the sys.argv indices correct. I thought it was just an oversight.
May 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by wukexin | last post: by
2 posts views Thread by Nobody | last post: by
7 posts views Thread by David. E. Goble | last post: by
1 post views Thread by cylin | last post: by
6 posts views Thread by shawn | last post: by
15 posts views Thread by ben | last post: by
15 posts views Thread by XZ | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.