Python Forum
[PyQt] [Solved]Help Adding results from for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Help Adding results from for loop
#1
Hello,

I'm trying to multiply the Quantity*Price for each row then add them all up to get the total value of all the items I have.

This is what I have so far:
    def calculate_value(self):
        for row in range(self.InventoryDisplay.model().rowCount()):
            QuantityValue = float(self.InventoryDisplay.model().index(row, 2).data())
            PriceValue = float(self.InventoryDisplay.model().index(row, 3).data())
            QuanityPriceValue = QuantityValue*PriceValue
            print(QuanityPriceValue)
My output:
Output:
89.0 0.0 75.0 200.0 32.5 20.0 20.0 4000.0 100.0
The part I'm having trouble with is how do I add all my QuanityPriceValue's that were spit out from that for loop so I can get the combined total (In this case it should be: 4,516.5)?

Thanks in advance.
Reply
#2
I don't have the values ​​from your table, so a generic example

prices = [33, 34.55, 45.12]
quantities = [2, 1, 5]
total_price = 0.0

def calculate_value():
    total_price = 0.0
    for row in range(len(prices)):
        QuantityValue = float(prices[row])
        PriceValue = float(quantities[row])
        QuanityPriceValue = QuantityValue*PriceValue
        print("QuanityPriceValue:", QuanityPriceValue)
        total_price += QuanityPriceValue
    print("total price:",total_price)

calculate_value()  
Output:
QuanityPriceValue: 66.0 QuanityPriceValue: 34.55 QuanityPriceValue: 225.6 total price: 326.15
Reply
#3
(Jun-24-2022, 04:56 PM)Axel_Erfurt Wrote: I don't have the values ​​from your table, so a generic example

prices = [33, 34.55, 45.12]
quantities = [2, 1, 5]
total_price = 0.0

def calculate_value():
    total_price = 0.0
    for row in range(len(prices)):
        QuantityValue = float(prices[row])
        PriceValue = float(quantities[row])
        QuanityPriceValue = QuantityValue*PriceValue
        print("QuanityPriceValue:", QuanityPriceValue)
        total_price += QuanityPriceValue
    print("total price:",total_price)

calculate_value()  
Output:
QuanityPriceValue: 66.0 QuanityPriceValue: 34.55 QuanityPriceValue: 225.6 total price: 326.15


Thanks! That's exactly what I was looking for.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Help getting out of loop Extra 5 3,671 Sep-30-2022, 11:27 PM
Last Post: Extra
  [PyQt] [Solved]Populate ComboBox with for loop? Extra 3 2,056 Jul-31-2022, 09:01 PM
Last Post: Extra
  [PyQt] [Solved]Display PyQtTable results from A->Z & Z->A Extra 2 1,143 Jul-18-2022, 04:04 PM
Last Post: Extra
  [PyQt] [Solved]Help Adding Sql Results in ComboBox Extra 2 1,216 Jul-07-2022, 09:46 PM
Last Post: Extra
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,387 Jun-29-2022, 10:20 PM
Last Post: Extra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020