Re-write this Python code that counts down from 5 to 0 in a more efficient way:

import time
seconds = 5
print("Remaining time: " + str(seconds))
seconds = seconds - 1
time.sleep(1)
print("Remaining time: " + str(seconds))
seconds = seconds - 1
time.sleep(1)
print("Remaining time: " + str(seconds))
seconds = seconds - 1
time.sleep(1)
print("Remaining time: " + str(seconds))
seconds = seconds - 1
time.sleep(1)
print("Remaining time: " + str(seconds))
seconds = seconds - 1
time.sleep(1)
print("Remaining time: " + str(seconds))

Imagine you are writing a program that will be used to manage production at a cookie factory. Describe one object you will create; it should have at least two methods and three attributes.
Look at the following program. Is it procedural or object-oriented? What would you do to convert it to the other type?

song1 = "Let's Dance"
song1_info = [128, 34, 304]

song2 = "Party Time!"
song2_info = [144, 32, 439]

song3 = "My Dreams"
song3_info = [93, 41, 339]

Describe in your own words what this program does:

class Meals:
def __init__(self, name):
self.name = name

fish = Meals("salmon")
print(fish.name)

Write a Python module that solves a problem.

1 answer

1. Efficient Countdown Code

Here’s a more efficient way to count down from 5 to 0 using a loop:

import time

for seconds in range(5, -1, -1):
    print(f"Remaining time: {seconds}")
    time.sleep(1)

2. Object for Cookie Factory Management

In a cookie factory program, you might create a CookieBatch object with the following attributes and methods:

Attributes:

  1. batch_id: A unique identifier for each batch of cookies.
  2. cookie_type: The type of cookie being produced (e.g., chocolate chip, oatmeal raisin).
  3. quantity: The number of cookies in the batch.

Methods:

  1. bake(batch_time): Simulates the baking process for a specified amount of time.
  2. report_status(): Provides a summary of the batch, including batch ID, cookie type, and quantity.

3. Program Type Analysis

The given program is procedural because it operates on individual song variables and lists with no encapsulation or use of objects. To convert it to object-oriented programming, you could create a Song class to encapsulate the attributes and methods related to a song.

Here's an example of what that might look like:

class Song:
    def __init__(self, title, bpm, duration, file_size):
        self.title = title
        self.bpm = bpm
        self.duration = duration
        self.file_size = file_size

    def play(self):
        print(f"Playing '{self.title}' at {self.bpm} BPM.")

# Create song objects
song1 = Song("Let's Dance", 128, 34, 304)
song2 = Song("Party Time!", 144, 32, 439)
song3 = Song("My Dreams", 93, 41, 339)

# Example usage
song1.play()

4. Explanation of the Code

The class Meals is a simple example of object-oriented programming. When an object of the Meals class is instantiated, it initializes with a name for the meal. In the provided code, the fish object represents a meal called "salmon", and its name is then printed. Essentially, this program creates a meal instance and outputs its name.

5. Python Module to Solve a Problem

Here's a Python module that could solve a basic arithmetic problem by providing a simple calculator functionality:

# calculator.py

class Calculator:
    def add(self, a, b):
        return a + b

    def subtract(self, a, b):
        return a - b

    def multiply(self, a, b):
        return a * b

    def divide(self, a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero.")
        return a / b

if __name__ == "__main__":
    calc = Calculator()
    print("Add 5 + 3 =", calc.add(5, 3))
    print("Subtract 5 - 3 =", calc.subtract(5, 3))
    print("Multiply 5 * 3 =", calc.multiply(5, 3))
    print("Divide 5 / 3 =", calc.divide(5, 3))

This calculator.py module defines a Calculator class with methods to perform basic arithmetic operations. The if __name__ == "__main__": block allows for basic testing of the functionality when the module is run as a standalone script.