Skip to content

Latest commit

 

History

History
75 lines (65 loc) · 3.08 KB

File metadata and controls

75 lines (65 loc) · 3.08 KB

Homework 4: Lists, While Loops, and Input Validation

Instructions

Complete the following exercises using the code and concepts from workshop_4. All work should be committed to your GitHub repository.

1. List Operations

  • Create a Python script that:
    • Creates a list of at least 8 integers
    • Prints the list and its length
    • Calculates and prints the sum of all elements
    • Calculates and prints the average of all elements
    • Finds and prints the maximum and minimum values
  • Reference the approach shown in list.py

2. Temperature Analysis

  • Write a script that:
    • Creates a list of daily temperatures for a 7-day period
    • Uses a for loop with enumerate to print each day number and its temperature
    • Calculates and prints the average temperature
    • Identifies and prints the warmest and coldest days
  • Follow the pattern in list.py but implement your own solution

3. While Loop Implementation

  • Create a script that implements a number guessing game:
    • Generate a random number between 1 and 100 (use import random and random.randint(1, 100))
    • Use a while loop to allow the user to guess the number
    • Provide feedback after each guess ("too high" or "too low")
    • Count and display the number of guesses when the user guesses correctly
  • Reference the approach in while.py

4. Break and Continue

  • Write a script that:
    • Uses a loop to iterate through numbers from 1 to 50
    • Uses continue to skip multiples of 3
    • Uses break to exit the loop when a multiple of 7 that is greater than 30 is found
    • Prints appropriate messages during the execution
  • Follow the pattern shown in break_continue.py

5. Input Validation

  • Create a calculator program that:
    • Uses while loops for input validation
    • Asks for two numbers and an operation (+, -, *, /)
    • Validates that the inputs for numbers are actually numeric
    • Handles division by zero appropriately
    • Displays the result and asks if the user wants to perform another calculation
  • Reference the validation techniques in validation.py

6. List Manipulation (Bonus) [Requires Research]

  • Write a script that:
    • Creates a list of strings (e.g., names of friends)
    • Allows the user to:
      • Add a new name to the list
      • Remove a name from the list
      • Search for a name in the list
      • Display all names in alphabetical order
    • Implements proper input validation for all operations
    • Continues until the user chooses to exit
  • Use concepts from both list.py and validation.py

Submission Guidelines

  1. Create a folder named homework_4 in your GitHub repository.
  2. Create separate .py files for each exercise (6 files total).
  3. Include meaningful comments in your code explaining your logic.
  4. Make sure to commit and push your changes to GitHub.
  5. Test all your scripts to ensure they work as expected.

Evaluation Criteria

  • Proper use of lists and list operations
  • Effective implementation of while loops
  • Correct use of break and continue statements
  • Proper input validation techniques
  • Code organization and readability
  • Error handling
  • Following the submission guidelines