Python Forum
[PyGUI] [Solved]Replace attribute for Floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] [Solved]Replace attribute for Floats
#1
Hello,

I'm trying to change my MarkupValue based on a price range but I can't seem to get it working properly because there is no "replace" attribute for float:
Error:
sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}' AttributeError: 'float' object has no attribute 'replace'
Is there something else I can use for floats to get it working?

Thanks in advance.

 def calculate_sellprice(self):
        for row in range(self.InventoryDisplay.model().rowCount() - 1):
                sell_price = float(self.InventoryDisplay.model().index(row, 3).data())

                if sell_price < 49.99:
                    Markup = 1.50
                    print("Range: $0-49.99\n"
                    "Markup: 50%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

                elif 50 <= sell_price <= 149.99:
                    Markup = 1.45
                    print("Range: $50-149.99\n"
                    "Markup: 45%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

                elif 150 <= sell_price <= 999.99:
                    Markup = 1.35
                    print("Range: $150-999.99\n"
                    "Markup: 35%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

                elif 1000 <= sell_price <= 1999.99:
                    Markup = 1.30
                    print("Range: $1,000-1,999.99\n"
                    "Markup: 30%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

                elif 2000 <= sell_price <= 2999.99:
                    Markup = 1.20
                    print("Range: $2,000-2,999.99\n"
                    "Markup: 20%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

                elif 3000 <= sell_price <= 3999.99:
                    Markup = 1.15
                    print("Range: $3,000-3,999.99\n"
                    "Markup: 15%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

                elif sell_price > 4000 :
                    Markup = 1.10
                    print("Range: $4,000 or Higher\n"
                    "Markup: 10%")
                    sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                    self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)
Reply
#2
What is the purpose of the replace? Floats don't have "." or ",".
Reply
#3
Good point. I completely overlooked that.

Did sell_price = f'{sell_price * Markup:.2f}' and it works now.

Thanks.
Reply
#4
You are typing instead of thinking. A few moments of reflection made it obvious to you that floats do not have periods or commas. I think a few minutes looking at this code will also point out there is no need to specify a range for each elseif. This is typing without thinking:
               if sell_price < 49.99:
                   ...
               elif 50 <= sell_price <= 149.99:
                   ...
               elif 150 <= sell_price <= 999.99:
                   ...
               elif 1000 <= sell_price <= 1999.99:
                   ...
               elif 2000 <= sell_price <= 2999.99:
                   ...
               elif 3000 <= sell_price <= 3999.99:
                   ...
               elif sell_price > 4000 :
                   ...)
This is thinking and then typing:
               if sell_price < 50:
                   ...
               elif sell_price < 150:
                   ...
               elif sell_price < 1000
                   ...
               elif sell_price < 2000:
                   ...
               elif  sell_price < 3000:
                   ...
               elif sell_price < 4000:
                   ...
               else :
                   ...
You also cut and past without thinking about what you are doing. This appears in your function 7 times.
                   Markup = 1.50
                   sell_price = f'{float(sell_price.replace(",", ".")) * Markup:.2f}'
                   self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)
The only thing that changes from one price range to the next is the Markup. You should have only one line that computes the sell prices and one line that updates the inventory display. This is code that results from thinking instead of cut and paste. Not only is it shorter, but the sell price range isn't full of holes like the range in your code.
               if sell_price < 50:
                   Markup = 1.50
               elif sell_price < 150:
                   Markup = 1.45
               elif sell_price < 1000:
                   Markup = 1.35 
               elif sell_price < 2000:
                   Markup = 1.30
               elif sell_price < 3000:
                   Markup = 1.20
               elif sell_price <= 4000:
                   Markup = 1.15
               else:
                   Markup = 1.10

               sell_price = f'{sell_price * Markup:.2f}'
               self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)
Cut and past is not a programming tool. If you are cutting and pasting to make duplicate or near duplicate code you are doing something wrong. Stop. Take you hands away from the keyboard, and think.
Reply
#5
(Jun-10-2022, 12:29 AM)deanhystad Wrote: Floats don't have "." or ",".

It can happen with badly formatted databases, e.g. if there are strings in them.

For example, in Germany commas are used, instead of 5.12 they use 5,12
Reply
#6
It wish Extra would explain why a conversion was required. Was the table using comma as the decimal character? That would be better fixed using a localization setting instead converting the table entry to a string and hard coding the decimal.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,758 Aug-08-2020, 12:47 AM
Last Post: linuxhacker

Forum Jump:

User Panel Messages

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