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

User-input string contains certain character test

P: 34
All,
I will have a user input a string that is either 10, 14, 17 characters long, or contains a comma (of any length). The script proceeds from there depending on the length entered. When I test with the various acceptable lengths or containing a comma, the script works. However, if I put in an unacceptable length (and without a comma), I keep getting the 'to LL model' print statement instead of the 'Unacceptable entry -- bailing out!' statement. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. user_input = sys.argv[1]
  2.  
  3. if len(user_input) == 10:
  4.     #set
  5.     print "to X model"
  6. elif len(user_input) == 14:
  7.     #set
  8.     print "to T model"
  9. elif len(user_input) == 17:
  10.     #set
  11.     print "to MR model"
  12. elif user_input.find(','):
  13.     #set
  14.     print "to LL model"
  15. else:
  16.     print "Unacceptable entry -- bailing out!"
  17.  
Thanks in advance for any help!
Jan 7 '08 #1
Share this Question
Share on Google+
14 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
All,
I will have a user input a string that is either 10, 14, 17 characters long, or contains a comma (of any length). The script proceeds from there depending on the length entered. When I test with the various acceptable lengths or containing a comma, the script works. However, if I put in an unacceptable length (and without a comma), I keep getting the 'to LL model' print statement instead of the 'Unacceptable entry -- bailing out!' statement. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. user_input = sys.argv[1]
  2.  
  3. if len(user_input) == 10:
  4.     #set
  5.     print "to X model"
  6. elif len(user_input) == 14:
  7.     #set
  8.     print "to T model"
  9. elif len(user_input) == 17:
  10.     #set
  11.     print "to MR model"
  12. elif user_input.find(','):
  13.     #set
  14.     print "to LL model"
  15. else:
  16.     print "Unacceptable entry -- bailing out!"
  17.  
Thanks in advance for any help!
The string method find() returns -1 if the substring is not found, which will evaluate True. Use this instead:
Expand|Select|Wrap|Line Numbers
  1. elif ',' in user_input:
  2.  
Jan 7 '08 #2

P: 34
Ah, the explanation is appreciated, and that works! Thanks so much!!
Jan 7 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Ah, the explanation is appreciated, and that works! Thanks so much!!
Just as another example, I went another route, using Regular Expressions and did the following, which seemed to work fine:

Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. user_input = "111111111111"   # a string of 12 characters
  4.  
  5. if len(user_input) == 10:
  6.     print "to X model"
  7. elif len(user_input) == 14:
  8.     print "to T model"
  9. elif len(user_input) == 17:
  10.     print "to MR model"
  11. elif re.search(',', user_input):
  12.     print "to LL model"
  13. else:
  14.     print "Nothing matched!  Exiting!"
  15.  
I know its not the same as using find, but it gets you the result you expected.

Regards,

Jeff
Jan 7 '08 #4

Motoma
Expert 2.5K+
P: 3,235
Just as another example, I went another route, using Regular Expressions and did the following, which seemed to work fine:

Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. user_input = "111111111111"   # a string of 12 characters
  4.  
  5. if len(user_input) == 10:
  6.     print "to X model"
  7. elif len(user_input) == 14:
  8.     print "to T model"
  9. elif len(user_input) == 17:
  10.     print "to MR model"
  11. elif re.search(',', user_input):
  12.     print "to LL model"
  13. else:
  14.     print "Nothing matched!  Exiting!"
  15.  
I know its not the same as using find, but it gets you the result you expected.

Regards,

Jeff
And another way would be like so:


Expand|Select|Wrap|Line Numbers
  1. model = 'unknown'
  2. lookup = {10:'X', 14:'T', 17:'MR'}
  3.  
  4. if len(user_input) in lookup:
  5.   model = lookup[len(user_input)]
  6. elif ',' in user_input:
  7.   model = 'LL'
  8.  
  9. if model != unknown:
  10.   print 'to ' + model + ' model'
  11. else:
  12.   print 'Bad input, get lost!'
  13.  
Jan 7 '08 #5

bvdet
Expert Mod 2.5K+
P: 2,851
And another way would be like so:


Expand|Select|Wrap|Line Numbers
  1. model = 'unknown'
  2. lookup = {10:'X', 14:'T', 17:'MR'}
  3.  
  4. if len(user_input) in lookup:
  5.   model = lookup[len(user_input)]
  6. elif ',' in user_input:
  7.   model = 'LL'
  8.  
  9. if model != unknown:
  10.   print 'to ' + model + ' model'
  11. else:
  12.   print 'Bad input, get lost!'
  13.  
Since we are having some fun with this:
Expand|Select|Wrap|Line Numbers
  1. patt = re.compile(r',|\b\w{17}\b|\b\w{14}\b|\b\w{10}\b')
  2. lookup = {10:'X', 14:'T', 17:'MR', 1:'LL'}
  3. m = patt.search(user_input)
  4. if m:
  5.     print 'to %s model' % (lookup[len(m.group(0))])
  6. else:
  7.     print 'Bad input, get lost!'
Jan 7 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
Since we are having some fun with this:
Expand|Select|Wrap|Line Numbers
  1. patt = re.compile(r',|\b\w{17}\b|\b\w{14}\b|\b\w{10}\b')
  2. lookup = {10:'X', 14:'T', 17:'MR', 1:'LL'}
  3. m = patt.search(user_input)
  4. if m:
  5.     print 'to %s model' % (lookup[len(m.group(0))])
  6. else:
  7.     print 'Bad input, get lost!'
Oh look, we have a winner!!!

That is a nice bit of code there, bvdet!
Jan 7 '08 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Oh look, we have a winner!!!

That is a nice bit of code there, bvdet!
Thanks, but all I did is take something you and Motoma did and add something to it. This kind of stuff can be fun.
Jan 7 '08 #8

Motoma
Expert 2.5K+
P: 3,235
Since we are having some fun with this:
Expand|Select|Wrap|Line Numbers
  1. patt = re.compile(r',|\b\w{17}\b|\b\w{14}\b|\b\w{10}\b')
  2. lookup = {10:'X', 14:'T', 17:'MR', 1:'LL'}
  3. m = patt.search(user_input)
  4. if m:
  5.     print 'to %s model' % (lookup[len(m.group(0))])
  6. else:
  7.     print 'Bad input, get lost!'
Damn good, except it matches only alphanumeric characters. The other examples would accept !@#$%^&*:. as a string, but yours will give an error.

Just a note for those watching at home.
Jan 7 '08 #9

Motoma
Expert 2.5K+
P: 3,235
I wonder if our PERL expert (numberwhun) can whip up a one-liner for us in a regex :D
Jan 7 '08 #10

bvdet
Expert Mod 2.5K+
P: 2,851
Damn good, except it matches only alphanumeric characters. The other examples would accept !@#$%^&*:. as a string, but yours will give an error.

Just a note for those watching at home.
That's correct! \w only matches alphanumeric characters. I was thinking the intent was to input a word. The following will match any character except whitespace, but the first letter must be alphanumeric (or a comma).
Expand|Select|Wrap|Line Numbers
  1. patt2 = re.compile(r',|\b\S{17}\Z|\b\S{14}\Z|\b\S{10}\Z')
\b matches the word boundary. A word is defined as a series of alphanumeric characters. \Z matches the end of the string.
Jan 8 '08 #11

Motoma
Expert 2.5K+
P: 3,235
That's correct! \w only matches alphanumeric characters. I was thinking the intent was to input a word. The following will match any character except whitespace, but the first letter must be alphanumeric (or a comma).
Expand|Select|Wrap|Line Numbers
  1. patt2 = re.compile(r',|\b\S{17}\Z|\b\S{14}\Z|\b\S{10}\Z')
\b matches the word boundary. A word is defined as a series of alphanumeric characters. \Z matches the end of the string.
What about:
Expand|Select|Wrap|Line Numbers
  1. patt3 = re.compile(r',|^\S{17}$|^\S{14}$|^\S{10}$')
  2.  
?
Jan 8 '08 #12

numberwhun
Expert Mod 2.5K+
P: 3,503
What about:
Expand|Select|Wrap|Line Numbers
  1. patt3 = re.compile(r',|^\S{17}$|^\S{14}$|^\S{10}$')
  2.  
?
And you said we couldn't play "Golf" in Python! Really!
Jan 8 '08 #13

bvdet
Expert Mod 2.5K+
P: 2,851
What about:
Expand|Select|Wrap|Line Numbers
  1. patt3 = re.compile(r',|^\S{17}$|^\S{14}$|^\S{10}$')
  2.  
?
I think you've got it Motoma! Character '^' matches the start of a string and '$' matches the end.
Jan 8 '08 #14

P: 34
Wow, thanks everyone for the numerous suggestions. Much of the latter stuff is way over my head, but its still good learning information! Glad everyone is having fun with my simple-to-you-all question.
Jan 8 '08 #15

Post your reply

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