-2

What I need is to print the total a person has spent on products, I know the code is horrible but that's how I received the assignment. Never worked with Python so it's all a little mystery for me.

My result so far

enter image description here

The outcome should be 950 for Jeroen, 1175 for Martijn and 800 for Bart without printing them individually.

#start opdracht3 class
class opdracht3:

#start product class
class Product:
    #constructor
    def __init__(self, productname, price):
        self.productname = productname
        self.price = price
#end product class

#person class
class Person:       
    #constructor
    def __init__(self, name, email, productlist):
        self.name = name
        self.email = email
        self.productlist = productlist  

    #adding products to person's collection
    def buyItem(self, item):            
        self.productlist.append(item)
#end person class

#collection of persons
persons = []

#defining person1 with the items he has bought
productlist1 = []
person1 = Person("Jeroen","jbm.mutsters@avans.nl", productlist1)
product1 = Product("Moto G7",150)
person1.buyItem(product1)
product3 = Product("iPhone",800)
person1.buyItem(product3)

#defining person2 with the items he has bought
productlist2 = []
person2 = Person("Martijn","m.dereus1@avans.nl", productlist2)
product2 = Product("iPhone",800)
person2.buyItem(product2)
product5 = Product("iPad",375)
person2.buyItem(product5)

#defining person2 with the items he has bought
productlist3 = []
person3 = Person("Bart","b.linsen@avans.nl", productlist3)
product4 = Product("iPhone",800)
person3.buyItem(product2)

#add person1 and person2 to the persons collection
persons.append(person1)
persons.append(person2)
persons.append(person3)

#generating output
for p in persons:
    print(p.name)       
    for i in p.productlist:         
        print(i.productname)
        print(i.price)
    print("-----------------")      
print("einde output")
print("***************")
#end generating output

#end opdracht3 class

Thanks in advance.

peer
  • 1,001
  • 4
  • 13
  • 29
  • Does this answer your question? [Summing elements in a list](https://stackoverflow.com/questions/11344827/summing-elements-in-a-list) – wjandrea Jan 23 '20 at 17:51
  • Does this answer your question? [Sum a list of numbers in Python](https://stackoverflow.com/questions/4362586/sum-a-list-of-numbers-in-python) – AMC Jan 23 '20 at 23:39
  • Also, variable and function names should follow the `lower_case_with_underscores` style. – AMC Jan 23 '20 at 23:40

3 Answers3

3

You can use the built-in sum to find the sum, and a list comprehension to get the prices from the items:

sum([x.price for x in p.productlist])
Simon Crane
  • 2,122
  • 2
  • 10
  • 21
1

Same but in as a instance method:

class Person:
    def __init__(self, name, email, productlist):
        self.name = name
        self.email = email
        self.productlist = productlist

    def buyItem(self, item):
        self.productlist.append(item)

    def get_sum_spend(self):
        return sum([product.price for product in self.productlist])

Also, camel case methods naming typically is not used in Python. You can read more in pep8.

Charnel
  • 4,222
  • 2
  • 16
  • 28
0

I added the method sum_product_prices to the person class which adds up the prices of the products in the persons productlist. Add the persons to the list persons and print out the return value of sum_product_prices. I removed the opdracht 3 class because it is not used.

#start product class
class Product:
    #constructor
    def __init__(self, productname, price):
        self.productname = productname
        self.price = price
#end product class

#person class
class Person:
    #constructor
    def __init__(self, name, email, productlist):
        self.name = name
        self.email = email
        self.productlist = productlist

    #adding products to person's collection
    def buy_item(self, item):
        self.productlist.append(item)

    def sum_product_prices(self):
        sum = 0
        for product in self.productlist:
            sum += product.price
        return sum

#end person class

#collection of persons
persons = []

#defining person1 with the items he has bought
productlist1 = []
person1 = Person("Jeroen","jbm.mutsters@avans.nl", productlist1)
product1 = Product("Moto G7",150)
person1.buy_item(product1)
product3 = Product("iPhone",800)
person1.buy_item(product3)
persons.append(person1)

productlist2 = []
person2 = (Person("Martijn","x@y.com",productlist2))
person2.buy_item(product3)
product4 = Product("iPad",375)
person2.buy_item(product4)
persons.append(person2)

productlist3 = []
person3 = Person("Bart","a@b.com",productlist3)
person3.buy_item(product4)
persons.append(person3)

for person in persons:
    print(person.name + " " + str(person.sum_product_prices()))

Osvald Laurits
  • 1,228
  • 2
  • 17
  • 32