Skip to Content
Lab 2

Strings, String and List Methods, Exceptions

Overview and Objectives

In this lab you will work with Python strings and lists, and learn how to handle exceptions effectively. You will:

  • Manipulate a user-input sentence by applying various string methods.
  • Create a menu-driven program to manage a list of integers with robust error handling.
  • Implement a custom exception scenario with an age validator function.

By the end of this lab, you will be able to perform common string operations, manage list data with input validation, and raise/catch exceptions in your programs.

Instructions and Tasks

Task 1: String Manipulation Challenge

  1. Write a Python script (e.g., string_manipulation.py) that:
    • Prompts the user to enter a sentence.
    • Converts the entire sentence to uppercase and prints the result.
    • Prints the sentence in reverse order.
    • Counts and displays the number of vowels (a, e, i, o, u, regardless of case) present in the sentence.
    • Replaces every space in the sentence with a hyphen (-) and prints the modified string.
  2. Ensure that each output is clearly labeled (for example, “Uppercase:”, “Reversed:”, etc.).

Task 2: List Management with Error Handling

  1. Write a Python script (e.g., list_manager.py) that manages a list of integers using a text-based menu. The menu should offer the following options:
    • (a) Add a number: Prompt the user for an integer and append it to the list.
    • (b) Remove a number: Prompt for an index and attempt to remove the element at that index using list.pop(index).
    • (c) Display the list: Print the current list of numbers.
    • (d) Quit: Exit the program.
  2. Use a loop to continuously display the menu until the user chooses to quit.
  3. Implement error handling using try/except:
    • Catch errors when a non-integer is entered for the number or index.
    • Catch an IndexError if the user provides an invalid index for removal.
  4. Display clear error messages for any exceptions without terminating the program abruptly.

Task 3: Custom Exception Test (Age Validator)

  1. Write a function validate_age(age) that:
    • Accepts a parameter age.
    • Raises a ValueError with an appropriate message if the age is not between 0 and 120 (inclusive).
  2. Write a short script (e.g., age_validator.py) that:
    • Prompts the user to enter their age.
    • Calls validate_age with the input value.
    • Uses try/except to catch the exception if the age is invalid, printing the error message.
    • If no exception is raised, prints a message indicating that the age is accepted.

Submission

Submit the following files via a link to a single GitHub repository containing all the files.:

  • string_manipulation.py
  • list_manager.py
  • age_validator.py

Include sample outputs (screenshots or text logs) showing that each script works as intended.

Grading Rubric

CriteriaPointsDescription
String Operations15 ptsThe program correctly performs all four string operations (uppercase, reverse, vowel count, replace spaces) and prints the results with clear labels. Minor errors in any operation will result in partial credit.
List Management Functionality15 ptsThe list menu program correctly adds, removes, and displays list items. Full credit if all menu options function as specified; partial credit for minor issues in one or more operations.
Exception Handling in List Management10 ptsThe program properly uses try/except to handle non-integer input and invalid index errors during removal. Full credit if both error types are handled gracefully; partial credit if only one type is caught.
Custom Exception Usage (Age Validator)5 ptsThe validate_age function correctly raises a ValueError for ages outside 0–120, and the calling code catches the exception and prints an appropriate error message.
Code Organization and Style5 ptsCode is well-organized and includes clear variable names, appropriate function definitions, and sufficient comments. Formatting and output clarity contribute to this score.

Total Points: 50 points

Note

Partial credit will be awarded for submissions that demonstrate a clear understanding of the concepts and correct implementation of some, but not all, required elements. If a solution is incomplete or contains minor errors (such as a slight miscalculation in vowel counting, an off-by-one error in list management, or missing one of the try/except cases), evaluators may award partial points proportional to the work correctly completed. Points may be deducted for errors in logic, formatting, or missing features; however, credit will be given where the underlying approach is sound and the code shows a genuine attempt to meet the rubric criteria.