471,354 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

User-input string contains certain character test

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
14 4557
bvdet
2,851 Expert Mod 2GB
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
jld730
34
Ah, the explanation is appreciated, and that works! Thanks so much!!
Jan 7 '08 #3
numberwhun
3,503 Expert Mod 2GB
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
3,237 Expert 2GB
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
2,851 Expert Mod 2GB
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
3,503 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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
3,237 Expert 2GB
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
3,237 Expert 2GB
I wonder if our PERL expert (numberwhun) can whip up a one-liner for us in a regex :D
Jan 7 '08 #10
bvdet
2,851 Expert Mod 2GB
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
3,237 Expert 2GB
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
3,503 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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
jld730
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.

Similar topics

3 posts views Thread by Ben Binskin | last post: by
1 post views Thread by arabub | last post: by
3 posts views Thread by Jorge Pinto | last post: by
1 post views Thread by DC_DBA | last post: by
2 posts views Thread by Tom Loach | last post: by
reply views Thread by Renato Neves | last post: by
3 posts views Thread by siliconmike | last post: by
2 posts views Thread by Sandman | last post: by
1 post views Thread by Rudolf Bargholz | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.