Build the ItemToPurchase class with the following specifications: <Please solve
ID: 3776257 • Letter: B
Question
Build the ItemToPurchase class with the following specifications: <Please solve in Python>
Attributes (3 pts)
item_name (string)
item_price (float)
item_quantity (int)
Default constructor (1 pt)
Initializes item's name = "none", item's price = 0, item's quantity = 0
Method
print_item_cost()
Ex. of print_item_cost() output:
(2) In the main section of your code, prompt the user for two items and create two objects of the ItemToPurchase class. (2 pts)
Ex:
(3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
Explanation / Answer
class ItemToPurchase(object):
'Items to Purchase class'
#default constructor with 3 parameters
def __init__(self,item_name="none",item_price=0,item_quantity=0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
#print item method
def print_item_cost(self):
print self.item_name,self.item_quantity,"@ $",self.item_price," = $",self.item_quantity*self.item_price
print "Item1"
n1 = raw_input("Enter the item name:")
p1 = input("Enter the item price:")
q1 = input("Enter the item quantity:")
# create item1 object
i1 = ItemToPurchase(n1,p1,q1)
print "Item2"
n2 = raw_input("Enter the item name:")
p2 = input("Enter the item price:")
q2 = input("Enter the item quantity:")
# create item2 object
i2 = ItemToPurchase(n2,p2,q2)
print "TOTAL COST"
i1.print_item_cost()
i2.print_item_cost()
print "Total:$",(i1.item_price*i1.item_quantity + i2.item_price*i2.item_quantity)
"""
sample output
Item1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1
Item2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10
TOTAL COST
Chocolate Chips 1 @ $ 3 = $ 3
Bottled Water 10 @ $ 1 = $ 10
Total:$ 13
"""
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.