1

I am working on a program to determine if a license plate is in the correct order using python 3.4 (I am beginning programming, and doing some self assigned home work).

The license plate should be in the order of three letters, and three numbers for correct order.

This is my code:

#Get data from user
plate = input('Enter the lisence plate number: ')
#Determine if style is old or new
if len(plate) == 6 and plate[0] >= "A" and plate[0] <= "Z"\
 and plate[1] >= "A" and plate[1] <= "Z"\
 and plate[2] >= "A" and plate[2] <= "Z"\
 and plate[3] >= "0" and plate[1] <= "9"\
 and plate[4] >= "0" and plate[4] <= "9"\
 and plate[5] >= "0" and plate[5] <= "9":
 verd = 'works'
else: 
 verd = 'Not work'
#print results
 print(verd)

When I enter the license plate ABC123, it is telling me that it doesn't work.

I have been trying everything, and can not figure out why this isn't working.

Any help would be appreciated.

asked Aug 17, 2015 at 1:22
2
  • correct order using python 3.4 Commented Aug 17, 2015 at 1:28
  • Aside from the regexp suggestion, you could also write that as: if len(plate) == 6 and plate[:3].isupper() and plate[3:].isdigit():... Commented Aug 17, 2015 at 1:45

4 Answers 4

3

By the way, the error in your method is in the third condition -

and plate[3] >= "0" and plate[1] <= "9" <-------- Notice that you are using `plate[1]` instead of [3]

Change it to -

and plate[3] >= "0" and plate[3] <= "9"
answered Aug 17, 2015 at 1:32
Sign up to request clarification or add additional context in comments.

1 Comment

By the way , when these kinds of condition go wrong, I usually print each condition separately , to find out which one is actually failing. Sometimes you can even find out the wrong one , just by trying to separate them.
1

A simple regex will do this job.

re.match(r'[A-Z]{3}[0-9]{3}$', s)

Since re.match tries to match from the begining of the string, you don't need to use start of the line anchor ^.

Example:

>>> import re
>>> def check(s):
 if re.match(r'[A-Z]{3}[0-9]{3}$', s):
 print('works')
 else:
 print('Not work')
>>> check(input('Enter the lisence plate number: '))
Enter the lisence plate number: ABC123
works
>>> check(input('Enter the lisence plate number: '))
Enter the lisence plate number: ABCDEF
Not work
>>> 
answered Aug 17, 2015 at 1:24

7 Comments

Thanks for the reply! I am not quite at that level yet.... I am using a work book, and am under the if statement section (brand new to coding). My solution matches the solution they give. Any idea on why this code isn't working?
Thanks for the education!
OMG, why 2 downvotes? Don't know what's wrong with alternate answer? @Downvoter whar if the length of the string is about 120 chars. No one willing to check for each char separately according to the position. Posted this comment not to get upvotes, but to know why..
No idea, I like the regex, +1 from me.
"whar if the length of the string is about 120 chars" --- it does not work like that. For this very task the string is of the fixed size and the OP is a beginner that needed help in understanding why their code is broken. Unless you're willing to write code for them forever - this answer does not address the OP's problem.
|
0

on line :

and plate[3] >= "0" and plate[1] <= "9"\

change '1' to '3' like this:

and plate[3] >= "0" and plate[3] <= "9"\
answered Aug 17, 2015 at 1:36

1 Comment

well, it took me 10 minutes to figure this out... by the time i answered, someone else answered.
0

Your bug is a typo.

 and plate[3] >= "0" and plate[1] <= "9"\

versus

 and plate[3] >= "0" and plate[3] <= "9"\

Also, the indentation of print(verd) is off; it's in the else block, so it would only print anything if the license wasn't valid.

Here's your code with minimal changes to get it working:

#Get data from user
plate = input('Enter the lisence plate number: ')
#Determine if style is old or new
if len(plate) == 6 and plate[0] >= "A" and plate[0] <= "Z"\
 and plate[1] >= "A" and plate[1] <= "Z"\
 and plate[2] >= "A" and plate[2] <= "Z"\
 and plate[3] >= "0" and plate[3] <= "9"\ # Fixed bug here
 and plate[4] >= "0" and plate[4] <= "9"\
 and plate[5] >= "0" and plate[5] <= "9":
 verd = 'works'
else:
 verd = 'Not work'
#print results
print(verd) # Fixed bug here

However, your code has a few other issues, so here's an improved version:

# Get data from user
plate = input('Enter the license plate number: ')
# Determine if style is old or new
if (len(plate) == 6 and
 'A' <= plate[0] <= 'Z' and
 'A' <= plate[1] <= 'Z' and
 'A' <= plate[2] <= 'Z' and
 '0' <= plate[3] <= '9' and
 '0' <= plate[4] <= '9' and
 '0' <= plate[5] <= '9'):
 verd = 'Valid'
else:
 verd = 'Invalid'
print(verd)

You could also do this:

# Get data from user
plate = input('Enter the license plate number: ')
# Determine if style is old or new
if (len(plate) == 6 and
 plate[:3].isupper() and plate[:3].isalpha() and
 plate[3:].isdigit():
 verd = 'Valid'
else:
 verd = 'Invalid'
print(verd)

But generally, I would say the best/cleanest way is the regular expression method:

import re
plate = input('Enter the license plate number: ')
if re.match(r'^[A-Z]{3}[0-9]{3}$', plate):
 print('Valid')
else:
 print('Invalid')
answered Aug 17, 2015 at 1:44

1 Comment

Thanks man, very helpful, I didn't think you could do some of that sentax

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.