In: Computer Science
# Write three functions to return the pieces of a string containing
# a city, state, and zip code.
# This function needs a better comment
def get_zip(city_state_zip):
return city_state_zip[-1]
# This function needs a better comment
def get_city(city_state_zip):
return city_state_zip[0:2]
# This function needs a better comment
def get_state(city_state_zip):
return 'Confusion'
def main():
place1 = 'Rolla, MO 65402'
place2 = 'Cape Girardeau, MO 63780'
place3 = 'St. Louis,MO63111'
place4 = 'International Falls,MN 56650 '
# Print the individual components
print ('City:', get_city(place1))
print ('State:', get_state(place1))
print ('Zip:', get_zip(place1))
# Repeat printing for the rest of the placesmain()
Directions
Your assignment is to write 3 functions, each of which takes a single string parameter representing [city, state, and zip code] and returns the indicated piece of the string. The main program should call each function, and print the returned values, one per line. However, the data that is passed to the functions was input on a keyboard that had a temperamental space bar. Sometimes it doesn't work and other times it generates multiple spaces, so the data may look like
'Rolla,MO 65402'
or
'Springfield, MO65897'
The one thing that is consistent is that there is a comma after the city name, the state is always two upper case characters, and the zip code is 5 adjacent characters 0-9.
For the first case listed, your program should output:
City: Rolla State: MO Zip: 65402
To make things interesting, add two more variables with cities of your choice. Choose the spacing to help test your program.
CODE -
# This function extracts and returns the zip of a city from a string containing city name, state name, and zip code
def get_zip(city_state_zip):
city, state_zip = city_state_zip.split(",")
state_zip = state_zip.strip()
return state_zip[-5:]
# This function extracts and returns the name of a city from a string containing city name, state name, and zip code
def get_city(city_state_zip):
city, state_zip = city_state_zip.split(",")
city = city.strip()
return city
# This function extracts and returns the state name of a city from a string containing city name, state name, and zip code
def get_state(city_state_zip):
city, state_zip = city_state_zip.split(",")
state_zip = state_zip.strip()
return state_zip[0:2]
def main():
place1 = 'Rolla, MO 65402'
place2 = 'Cape Girardeau, MO 63780'
place3 = 'St. Louis,MO63111'
place4 = 'International Falls,MN 56650 '
place5 = 'Jefferson City, MO 65043'
place6 = 'Saint Paul, MN 55101'
# Print the individual components
print ('City:', get_city(place1))
print ('State:', get_state(place1))
print ('Zip:', get_zip(place1))
print ('City:', get_city(place2))
print ('State:', get_state(place2))
print ('Zip:', get_zip(place2))
print ('City:', get_city(place3))
print ('State:', get_state(place3))
print ('Zip:', get_zip(place3))
print ('City:', get_city(place4))
print ('State:', get_state(place4))
print ('Zip:', get_zip(place4))
print ('City:', get_city(place5))
print ('State:', get_state(place5))
print ('Zip:', get_zip(place5))
print ('City:', get_city(place6))
print ('State:', get_state(place6))
print ('Zip:', get_zip(place6))
main()
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.