DntMesArnd
DntMesArnd

Reputation: 125

if statement not reading characters in string

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.

Upvotes: 1

Views: 57

Answers (4)

Cyphase
Cyphase

Reputation: 12022

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')

Upvotes: 0

Awesome_girl
Awesome_girl

Reputation: 484

on line :

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

change '1' to '3' like this:

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

Upvotes: 0

Anand S Kumar
Anand S Kumar

Reputation: 90989

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"

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174806

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
>>> 

Upvotes: 1

Related Questions