By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

What is wrong with this use of sys.argv

Deathwing
P: 32
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
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

Deathwing
P: 32
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
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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.