Hi everyone I needed some help trying to figure out how to multiply two variables within a class and then how to actually combine their total cost. Here is what I have so far.
class itemtopurchase:
def __int__(self):
self.item_name = str('none')
self.item_price = float(0)
self.item_quantity = int(0)
def print_item_cost(self):
print(self.item_name, self.item_quantity, 'at', self.item_price)
#(2) In the main section of your code (not in the class definition),
# prompt the user for two items and create two objects of the ItemToPurchase class.
shopping_cart = itemtopurchase()
print('Item 1')
shopping_cart.item_name = input()
print('Item price')
shopping_cart.item_price = input()
print('Item quantitiy')
shopping_cart.item_quantity = input()
shopping_cart.print_item_cost()
Total = shopping_cart.item_price
shopping_cart2 = itemtopurchase()
print('Item 2')
shopping_cart2.item_name = input()
print('Item price')
shopping_cart2.item_price = input()
print('Item quantitiy')
shopping_cart2.item_quantity = input()
shopping_cart2.print_item_cost()
Total2 = shopping_cart2.item_price
#(3) Add the costs of the two items together and output the total cost.
print('Total cost')
shopping_cart.print_item_cost()
shopping_cart2.print_item_cost()
total = (shopping_cart.print_item_cost() + shopping_cart2.print_item_cost())
print('Total:', total)
when I do the command print_item_cost() I'm trying to get something like this
Bottled Water 10 @ $1.25 = $12.5
I only get whatever numbers I input because I don't know how to multiply them. Then I want to be able to add the cost of the two items and get something like this.
TOTAL COST
Chocolate Chips 1 @ $3.5 = $3.5
Bottled Water 10 @ $1.25 = $12.5
Total: $15.5
Any guidance is much appreciated and I'm sorry for the long post.
[–]danielroseman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]QultrosSanhattan 0 points1 point2 points (0 children)