Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

python - Trying to use a dictionary to validate a state 2 letter abbreviation

I am new to Python and I am trying to use a dictionary to validate a 2 letter state abbreviation. Every time I put in the state abbreviation it just exits my program and I am lost on how to get this to work. I am not getting any errors on the program it is just exiting prior before I need it to. I think that using a dictionary is the best way but I am not sure how to make it work. I need the program to validated the abbreviation and make sure it is only 2 characters.

import sys

print('--------------------------------------------------')
print('Welcome to the US Voter Registration System')

# processing phase
con = input('Do You Want to Continue: Yes Or No? ')

while con == 'Yes':
name = input('Please Enter Your First Name: ')
name2 = input('Please Enter Your Last Name: ')

#  Used to continue the program
con2 = input('Do You Want to Continue: Yes Or No? ')
if 'yes'.startswith(con2.lower()):
    print('Yes')
else:
    print('Thanks For Your Time!')
    sys.exit()  #  end the loop
#  Used to get the age of the voter
age = int(input('Please Enter Your Age *** YOU MUST BE 18 OR OLDER ***: 
'))
if age < 18:
    print('Must be over 18!')
    sys.exit()  #  end the loop

if age >= 120:
    print('No one is this old! Try again! ')
    sys.exit()  #  end the loop
#  Used to continue the program
con2 = input('Do You Want to Continue: Yes Or No? ')
if 'yes'.startswith(con2.lower()):
    print('Yes')
else:
    print('Thanks For Your Time!')
    sys.exit()  # end the loop
#  Used to find out if the voter is a citizen or not
cit = input('Are You A US Citizen? Answer Yes or No: ')
if cit != 'Yes':
    print('You Must Be A US Citizen!')
    sys.exit()  #  end the loop
#  Used to continue the program
con2 = input('Do You Want to Continue: Yes Or No? ')
if 'yes'.startswith(con2.lower()):
    print('Yes')
else:
    print('Thanks For Your Time!')
    sys.exit()  # end the loop
#  Used to get the state of the voter
state = input('Please Enter Your State? ' 'Please only 2 letters: ')

state2 = {
    'Alabama': 'AL',
    'Alaska': 'AK',
    'American Samoa': 'AS',
    'Arizona': 'AZ',
    'Arkansas': 'AR',
    'California': 'CA',
    'Colorado': 'CO',
    'Connecticut': 'CT',
    'Delaware': 'DE',
    'District of Columbia': 'DC',
    'Florida': 'FL',
    'Georgia': 'GA',
    'Guam': 'GU',
    'Hawaii': 'HI',
    'Idaho': 'ID',
    'Illinois': 'IL',
    'Indiana': 'IN',
    'Iowa': 'IA',
    'Kansas': 'KS',
    'Kentucky': 'KY',
    'Louisiana': 'LA',
    'Maine': 'ME',
    'Maryland': 'MD',
    'Massachusetts': 'MA',
    'Michigan': 'MI',
    'Minnesota': 'MN',
    'Mississippi': 'MS',
    'Missouri': 'MO',
    'Montana': 'MT',
    'Nebraska': 'NE',
    'Nevada': 'NV',
    'New Hampshire': 'NH',
    'New Jersey': 'NJ',
    'New Mexico': 'NM',
    'New York': 'NY',
    'North Carolina': 'NC',
    'North Dakota': 'ND',
    'Northern Mariana Islands': 'MP',
    'Ohio': 'OH',
    'Oklahoma': 'OK',
    'Oregon': 'OR',
    'Pennsylvania': 'PA',
    'Puerto Rico': 'PR',
    'Rhode Island': 'RI',
    'South Carolina': 'SC',
    'South Dakota': 'SD',
    'Tennessee': 'TN',
    'Texas': 'TX',
    'Utah': 'UT',
    'Vermont': 'VT',
    'Virgin Islands': 'VI',
    'Virginia': 'VA',
    'Washington': 'WA',
    'West Virginia': 'WV',
    'Wisconsin': 'WI',
    'Wyoming': 'WY'
}

if state != state2.get:
    print('Please Enter One Of The 50 States!')
    sys.exit()  #  end the loop

#  Used to continue the program
con2 = input('Do You Want to Continue: Yes Or No? ')
if 'yes'.startswith(con2.lower()):
    print('Yes')
else:
    print('Thanks For Your Time!')
    sys.exit()  # end the loop
#  Used to get the voters zip code
zipc = int(input('Please Enter Your Zip Code? '))
if len(zipc) != 5:
    print('You Must Enter A Vaild Zip Code!')
    sys.exit()  #  end the loop

#  Used once you are done entering data
con = input('You Are Finished Please Type Done: ')

#  Output process
print('---------------------------------------------------------')
print('Thank You For Using the US Voter Registration System ')
print('Below Is A Summary Of Your Data: ')
print('NAME: ', name, name2)
print('AGE: ', age)
print('US CITIZEN:', cit)
print('STATE: ', state)
print('ZIP CODE: ', zipc)
print('Thank You For Using the US Voter Registration System ')
print('Please Check Your Mail For More Voting Information ')
print('---------------------------------------------------------------')

# Empty Line
question from:https://stackoverflow.com/questions/65713787/trying-to-use-a-dictionary-to-validate-a-state-2-letter-abbreviation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem might lie at

if state != state2.get:

The above code is checking value of string to that of inbuilt function since state2.get is an inbuilt function. This always evaluates to True since string is never equal to function and hence exiting the program.

You might better off be using

if state in state2.values():

state2.values() gives only the values of dictionary

And since you said you want to make sure the state entered is 2 characters, you can use len() as you used with zipc.

if len(state)!=2:

Furthur more, it seems that the full form of states aren't being used anywhere, if you only want to make sure that entered state is from valid list of states then you can just create a list having abbrevations of all the valid states and check whether entered input is in the list.

state2=['AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'GU', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VI', 'VA', 'WA', 'WV', 'WI', 'WY']
if state not in state2:
            sys.exit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...