Python Script for Number Collection and Counting

Answered using GPT-4o mini by AI Answer Generator

Question:

You will be submitting a single python file named Lastname_Initials_H10.py, that includes a “main” which calls all of your programs. • When I run your code, the programs should execute one after the next, without stopping. Do NOT import any libraries/modules for this assignment. Use lists and loops. • Your file should include: • Commented assignment information at the top: name, course, date, etc When each program runs, it should first print out a program header statement with the program number and a brief description of what the program does Read the entire assignment first. • There are a number of programs, although you will find they do many of the same things. Identify any tasks that are required in more than one program, such as getting inputs, determining the data type of an input, etc. Try to make a single common function that can be used in all the programs to perform that task. • As you should be building the programs individually, you will need to include these common functions as you build each program. However, when the programs are all put together in one file, there only needs to be one definition of these common functions which can then be called by multiple programs. Create a program that allows a user to enter any number of integers and/or floats, store them as a list, and then print those inputs as both 1) a single list and 2) values printed in a column with one number per line, indented to a single tab. The user may continue entering values until entering the string ‘done.’ The user may ONLY enter numbers, except for the sentinel ‘done.’ The program must include at least 5 functions:

  1. An appropriately named function that runs the program including a finished statement.
  2. a program header
  3. a function that gets user inputs while ensuring those inputs are numeric, and returns the number cast as either an int or a float as appropriate.
  4. a function that builds a list one element at a time and then returns the list.
  5. a function that takes a list as an argument and prints the list elements as 1) a single list and 2) in a column, one item per line, indented to a single tab. E.g., if the user enters the number 3, 2.9, and 8.41, the program should print: Your numbers in a list are [3, 2.9, 8.41] The numbers you entered are: 3 2.9 8.41. Program 4: “Count the Negative Numbers in List” Create a program that allows a user to enter any number of integers and/or floats, store them as a list, and then print the list of numbers and the count of elements that are negative numbers. The user may continue entering values until entering the sentinel value which must be the string ‘done.’ The user may ONLY enter numbers, except for the sentinel ‘done.’ The program must include at least 4 functions:
  6. An appropriately named function that runs the program including a finished statement.
  7. a program header
  8. a function that gets user inputs, ensuring those inputs are numeric, puts them into a list, and returns the list
  9. a function that takes a list as an argument and returns the count of list elements that are negative E.g., if the user enters the number 3, -2.9, 8.4, and -5 then the program should print: Your inputs are [3, -2.9, 8.4, -5] You entered 2 negative numbers.

Create a program that allows a user to enter any number of integers and/or floats, store them as a list, and then print those inputs as both 1) a single list and 2) values printed in a column with one number per line, indented to a single tab. The user may continue entering values until entering the string ‘done.’ The user may ONLY enter numbers, except for the sentinel ‘done.’ The program must include at least 5 functions:

  1. An appropriately named function that runs the program including a finished statement.
  2. a program header
  3. a function that gets user inputs while ensuring those inputs are numeric, and returns the number cast as either an int or a float as appropriate.
  4. a function that builds a list one element at a time and then returns the list.
  5. a function that takes a list as an argument and prints the list elements as 1) a single list and 2) in a column, one item per line, indented to a single tab. E.g., if the user enters the number 3, 2.9, and 8.41, the program should print: Your numbers in a list are [3, 2.9, 8.41] The numbers you entered are: 3 2.9 8.41. Program 4: “Count the Negative Numbers in List” Create a program that allows a user to enter any number of integers and/or floats, store them as a list, and then print the list of numbers and the count of elements that are negative numbers. The user may continue entering values until entering the sentinel value which must be the string ‘done.’ The user may ONLY enter numbers, except for the sentinel ‘done.’ The program must include at least 4 functions:
  6. An appropriately named function that runs the program including a finished statement.
  7. a program header
  8. a function that gets user inputs, ensuring those inputs are numeric, puts them into a list, and returns the list
  9. a function that takes a list as an argument and returns the count of list elements that are negative E.g., if the user enters the number 3, -2.9, 8.4, and -5 then the program should print: Your inputs are [3, -2.9, 8.4, -5] You entered 2 negative numbers.
Answer:

Here's a Python script that meets the requirements outlined in your assignment. The script includes three programs: one for collecting and displaying numbers, and another for counting negative numbers. It also includes common functions to handle user input and list operations.

# Lastname_Initials_H10.py
# Name: Your Name
# Course: Your Course
# Date: Today's Date

def main():
    program_one()
    program_two()

def program_one():
    print("Program 1: Collect and Display Numbers")
    numbers = get_numbers()
    print_numbers(numbers)
    print("Finished Program 1.")

def program_two():
    print("Program 2: Count the Negative Numbers in List")
    numbers = get_numbers()
    negative_count = count_negatives(numbers)
    print(f"Your inputs are {numbers}")
    print(f"You entered {negative_count} negative numbers.")
    print("Finished Program 2.")

def get_numbers():
    numbers = []
    while True:
        user_input = input("Enter a number (or 'done' to finish): ")
        if user_input.lower() == 'done':
            break
        try:
            number = float(user_input)  # Try to convert to float
            numbers.append(number)
        except ValueError:
            print("Invalid input. Please enter a number.")
    return numbers

def print_numbers(numbers):
    print(f"Your numbers in a list are {numbers}")
    print("The numbers you entered are:")
    for number in numbers:
        print(f"\t{number}")

def count_negatives(numbers):
    count = 0
    for number in numbers:
        if number < 0:
            count += 1
    return count

if __name__ == "__main__":
    main()

Explanation of the Code:

  1. Main Function: The main() function orchestrates the execution of the two programs.
  2. Program One:
    • Collects numbers from the user and displays them in both list format and column format.
  3. Program Two:
    • Collects numbers and counts how many of them are negative, then displays the results.
  4. Common Functions:
    • get_numbers(): Handles user input, ensuring that only valid numbers are accepted. It returns a list of numbers.
    • print_numbers(numbers): Takes a list of numbers and prints them in the specified formats.
    • count_negatives(numbers): Counts and returns the number of negative numbers in the provided list.

Usage:

  • To run the program, save the code in a file named Lastname_Initials_H10.py and execute it in a Python environment. The user will be prompted to enter numbers, and the program will handle the input and output as specified.