199k views
1 vote
Write a program that creates a simple class definition for a "Product" object.

The class should use the __init()__ constructor method to receive three bits of information about the product as the object is created - (name, price, and discountPercent)
The class should contain two methods. One for returning the Discount Amount and the other for returning a Discounted Price
Inside the same .py file as your new Product class, write code to create two separate instances of a "product". Note: You can either hard-code the parameter values or you can prompt the user for input. Either is fine
Print the product information to the screen. You will access both arguments and methods to get the correct output.
The Discount Amount can be determined by dividing the discountPercent argument by 100 and multiplying that by the price
The Discounted Price can be calculated by subtracting the Discounted Amount from the price. Note call the above Discount Amount method from inside this method to use in your calculation rather than doing the above formula again.

1 Answer

3 votes

Final answer:

To create a simple class definition for a 'Product' object in Python, use the __init__() constructor method to receive three pieces of information about the product.

Define two methods for returning the Discount Amount and the Discounted Price. Create two instances of the 'Product' object and print their information.

Step-by-step explanation:

To create a simple class definition for a 'Product' object in Python, you can use the __init__() constructor method. This method should receive three pieces of information about the product (name, price, and discountPercent) as parameters. Inside the class, you can define two methods: one for returning the Discount Amount and another for returning the Discounted Price.

  1. To calculate the Discount Amount, you can divide the discountPercent argument by 100 and multiply it by the price.
  2. To calculate the Discounted Price, you can subtract the Discount Amount from the price. In this calculation, you can call the Discount Amount method from the Discounted Price method, instead of repeating the formula.

In the same .py file as your Product class, you can create two separate instances of the 'Product' object. You can either hard-code the parameter values or prompt the user for input.

For example:

class Product:

To print the product information to the screen, you can access the arguments and methods of the product instances. For example:

print(f'Product 1 - Name: {product1.name}, Discounted Price: {product1.get_discounted_price()}')

User Amethyst
by
7.4k points