12.9k views
4 votes
Save the file created in the Module 7 lab as "product.py" and create a new file named

Add a __str()__ method to the product class so that a formatted output of the name, price, and initial DiscountPercent are returned along with the calculated values for DiscountAmount and DicountPrice.
NOTE: Use a def __str__(self): method to format the output string as seen below. See pages 507-509 in the Gaddis book. Lines 36-37 of Program 10-9 (bankaccount2.py) shows an example in the book.
Create a new ".py" file using the naming convention for the deliverable mentioned at the top of the assignment
Use an import statement to gain access to your product class module file
Add a loop to the main() function and build as many objects as the user wants to by prompting the user for the 3 key attribute values necessary to create a product.
After creating an object it should be added to a list. The program should continue, and prompt the user for a choice to continue or not.
Once the loop has ended use a For In loop to print each object from within the list.

User Mushroom
by
8.3k points

1 Answer

3 votes

Final answer:

To add a __str__() method to the product class in the 'product.py' file that returns a formatted output of the name, price, and initial DiscountPercent along with calculated values for DiscountAmount and DiscountPrice, you can define the __str__() method within the Product class using string formatting. After adding the __str__() method, create a new file, import the product module, and use a loop to prompt the user for attribute values and create multiple objects. Add these objects to a list, and then use a for loop to print each object.

Step-by-step explanation:

Product Class and __str__() Method

The question is asking to add a __str__() method to the product class in the 'product.py' file. This method should return a formatted output of the name, price, and initial DiscountPercent, along with the calculated values for DiscountAmount and DiscountPrice.

To accomplish this, you can define the __str__() method within the Product class in the 'product.py' file. This method should use string formatting to concatenate the necessary information and return it as a single string. Here's an example implementation:

def __str__(self):
return f"Name: {self.name}, Price: {self.price}, Initial Discount Percent: {self.initialDiscountPercent}, Discount Amount: {self.discountAmount()}, Discount Price: {self.discountPrice()}"

After adding the __str__() method, you can create a new file and import the product module using 'import product', then use a loop in the main() function to create multiple objects by prompting the user for the necessary attribute values. The created objects should be added to a list. Finally, use a for loop to print each object from the list.

User Tony Tarng
by
8.1k points