Creating Assignment Headers in Python

Answered using GPT-4o mini by AI Answer Generator

Question:

When a program is run, there should be something at the top of the output which explains to the user what the program is all about, and something at the bottom that says it is finished. For every assignment you submit, when it runs the first thing that should be output is an assignment header that includes the date, your name, the name of the course and the name of the assignment. Create a program with just strings that prints an assignment header bordered on the top and bottom with a line of symbols (symbol is your choice). Use concatenation and repetition as described in section 2.4.2. After the bottom border include two lines of print() to make blank lines as a spacer before the next line of output. An assignment header might look something like this:


Tony Stark 10/19/24 Computer Information Systems Course 2110, Section 002 - Structured Problem Solving Assignment – Project #2 Coding in Python


Program #2 – Enhance the assignment header with input, variables and escape characters: You’ll want to print out a program header that separates the individual programs within the assignment. Remember, the program output is being presented to a user who needs to be told what all these things are coming out on the screen. Include a blank line above and below the program header to offset it from the ending of the previous program and the start of the new program. The program header could look like this:

Program 2 – Enhanced Assignment Header

For this program create the header again but in this version use variables to capture input from the user for all elements except for the borders, and then print out the header using those variables. Also, use tabs and newlines instead of spaces and stand-alone print statements to align elements of the headers and create the multiple lines below the header. Learn about and use escape characters at https://www.w3schools.com/python/gloss_python_escape_characters.asp. Note that unless you cast them, inputs are always captured as strings, even if they are input as numbers like 2110 and 002. To practice converting between strings and numbers (section 2.4.3), convert your numeric inputs to numbers. Then add them and print out the sum, with an explanation of what the printed number is. A user should always understand what is being displayed on the screen, so in these assignments never just print out a number by itself. To help with this exercise and checking conversions, you probably want to use the type() function. Learn about that here https://www.geeksforgeeks.org/python-type-function/

Answer:

Certainly! Below is a Python program that meets the requirements you've outlined. The program first prints an assignment header with a border, and then it enhances the header by capturing user input for various elements. It also includes the use of escape characters for formatting.

# Program #1: Basic Assignment Header

# Define the border symbol
border_symbol = "*"
border_length = 40  # Length of the border

# Create the assignment header
header = (
    f"{border_symbol * border_length}\n"
    "Tony Stark\t10/19/24\n"
    "Computer Information Systems\n"
    "Course 2110, Section 002 - Structured Problem Solving\n"
    "Assignment – Project #2 Coding in Python\n"
    f"{border_symbol * border_length}\n"
)

# Print the header
print(header)

# Spacer
print("\n\n")

# Program #2: Enhanced Assignment Header
print("\n##### Program 2 – Enhanced Assignment Header #####\n")

# Capture user input
name = input("Enter your name: ")
date = input("Enter the date (MM/DD/YY): ")
course_name = input("Enter the course name: ")
course_number = input("Enter the course number (e.g., 2110): ")
section_number = input("Enter the section number (e.g., 002): ")
assignment_name = input("Enter the assignment name: ")

# Convert course and section numbers to integers for addition
course_number_int = int(course_number)
section_number_int = int(section_number)

# Create the enhanced assignment header using variables
enhanced_header = (
    f"{border_symbol * border_length}\n"
    f"{name}\t{date}\n"
    f"{course_name}\n"
    f"Course {course_number_int}, Section {section_number_int} - Structured Problem Solving\n"
    f"Assignment – {assignment_name}\n"
    f"{border_symbol * border_length}\n"
)

# Print the enhanced header
print(enhanced_header)

# Calculate and print the sum of course and section numbers
sum_numbers = course_number_int + section_number_int
print(f"The sum of the course number and section number is: {sum_numbers}.\n")
print(f"Type of course number: {type(course_number_int)}")
print(f"Type of section number: {type(section_number_int)}")

# Indicate that the program is finished
print("Program finished.")

Explanation:

  1. Basic Assignment Header: The first part of the program creates a static assignment header using string concatenation and repetition. It uses a border made of asterisks (*).

  2. Enhanced Assignment Header: The second part prompts the user for their name, date, course name, course number, section number, and assignment name. It captures this input and constructs a new header using the provided values.

  3. Type Conversion: The program converts the course and section numbers from strings to integers to perform addition. It then prints the sum along with the types of the variables to demonstrate the conversion.

  4. Formatting: The program uses escape characters like \t for tabs and \n for new lines to format the output neatly.

  5. Completion Message: Finally, it prints a message indicating that the program has finished running.

You can run this program in a Python environment, and it will prompt you for input and display the formatted headers