asked 61.4k views
3 votes
In Python,

Your supervisor needs you to create a program that will automatically format mailing labels. Your company's business office has a file that contains customer names and addresses. They would like a program that will be able to read in all of those names and addresses and format them so that they fit on mailing labels. The maximum amount of information you can print on the labels is 5 lines with each line having a maximum length of 35 characters.

You will be creating the first prototype. This prototype will contain all of the functions necessary to format the label data; however, rather than read this data from a file, it will simply allow someone to enter the name and address information from their keyboard. Eventually, your program will be converted to read in information from the file.

Input each piece of the customer information:

last_name

first_name

middle_name

street_address

city

state

zip_code

The data should be formatted so that the label looks like this:

first_name middle_name last_name

street_address (** this information may take two lines to represent)

city, state, zip_code

Because different people from the business office have entered the customer data, not all of the data is formatted the same. Thus, your program should make sure to fix the data formatting problems which are as follows:

* The first, middle and last names may not be capitalized and all of them should be capitalized.

* Some zip codes contain 9 digits and some only contain 5 digits. Because of the space concern on the label, all zip codes should be converted to 5 digits.

Write a program that uses functions and the string functions and methods that you have learned about in class to format and print the mailing labels. Because your supervisor expects you to follow good programming practices and create functions that only perform one task, she asks you to create the following functions:

* a main function - this function is where you should have statements that input your data (unless you want to make a function to get the input), and call the other functions you create

* a function to fix the zip code format

* a function to format the names for the label. If the total length of the first/middle/last name is greater than 35 characters, remove the middle name from the label.

* a function to format the street address for the label. If the length of the street address is greater than 35 characters, separate the address into two lines. If you are splitting a word when you do this, make sure to include a dash ("-") on the first part of the word to note the continuation of the word. Make sure you add the dash into your consideration of the overall line length (i.e. the length cannot exceed 35 characters)

* a function that displays all of the lines for the label properly.

Use only functions and methods that we have covered in class or in ZyBooks. Make sure to build your program incrementally. By building one small part and then testing to make sure that part works will ultimately make the task easier.

II You should test your program so that each of your functions gets tested. For example, you could use the following information to test for a long name:

condaleza seraphina santísima-trinidad

6265 Apt. A1 Brockporter Spencerport Lane, Brockport, NY 14420-9716

m The purpose of this assignment is to help you become familiar writing and using functions and string processing tools. There are many benefits to using functions, as you know from your readings, but it takes practice to get accustomed to breaking up your code into functions and passing data between functions. String processing is critical to know because all communication with the program uses strings so knowing how to manipulate strings is very valuable.

Skills and Knowledge - Completing this assignment will help you practice skills that are essential to your success in this class and your professional life beyond. In this assignment you will:

* Plan and create and utilize several functions to help you gain proficiency.

* Utilize several different string processing functions and methods to help you gain experience in string processing.

asked
User Mbmcavoy
by
8.3k points

1 Answer

1 vote

Answer:

Here is a possible solution to the problem:

```python

def fix_zip_code(zip_code):

# Remove any non-digit characters

zip_code = ''.join(filter(str.isdigit, zip_code))

# Convert to 5 digits

if len(zip_code) > 5:

zip_code = zip_code[:5]

elif len(zip_code) < 5:

zip_code = zip_code.zfill(5)

return zip_code

def format_names(first_name, middle_name, last_name):

# Capitalize the first letter of each name

first_name = first_name.capitalize()

middle_name = middle_name.capitalize()

last_name = last_name.capitalize()

# Check if the total length of the names is greater than 35 characters

total_length = len(first_name) + len(middle_name) + len(last_name)

if total_length > 35:

# Remove the middle name

middle_name = ""

return first_name, middle_name, last_name

def format_address(street_address):

# Check if the length of the street address is greater than 35 characters

if len(street_address) > 35:

# Split the address into two lines

words = street_address.split()

line1 = ""

line2 = ""

current_line_length = 0

for word in words:

if current_line_length + len(word) <= 35:

# Add word to current line

line1 += word + " "

current_line_length += len(word) + 1

else:

# Add word to next line

line2 += word + " "

# Add dash to the last word on the first line

line1 = line1.rstrip().rsplit(" ", 1)

line1[1] = "-" + line1[1]

line1 = " ".join(line1)

# Remove any extra spaces

line1 = line1.strip()

line2 = line2.strip()

return line1, line2

else:

# Address fits in one line

return street_address, ""

def display_label(first_name, middle_name, last_name, address_line1, address_line2, city, state, zip_code):

print(first_name, middle_name, last_name)

print(address_line1)

print(address_line2)

print(city + ",", state + ",", zip_code)

def main():

# Get input from the user

last_name = input("Enter last name: ")

first_name = input("Enter first name: ")

middle_name = input("Enter middle name: ")

street_address = input("Enter street address: ")

city = input("Enter city: ")

state = input("Enter state: ")

zip_code = input("Enter zip code: ")

# Format the data

zip_code = fix_zip_code(zip_code)

first_name, middle_name, last_name = format_names(first_name, middle_name, last_name)

address_line1, address_line2 = format_address(street_address)

# Display the label

display_label(first_name, middle_name, last_name, address_line1, address_line2, city, state, zip_code)

# Call the main function

main()

```

This program uses the `input()` function to get the user's input for the customer information. It then calls the necessary functions to format the data and displays the formatted mailing label using the `print()` function.

You can run this program and enter the customer information from your keyboard to see the formatted mailing label.

answered
User Sumon
by
9.4k points