Python Forum

Full Version: syntax for endswith a digit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
in a small loop, I am trying to sum all of the lineEdits (in a PyQt5 GUI) whose objectName endswith a number in a particular frame.  So far, I have the following code, but it has an AtributeError.
    def sumcar(self):
        amts = []
        for child in self.ui.frame_10.findChildren(QLineEdit):
            if child.objectName().endswith.isdigit():
                amt = int(child.text()) if child.text() else 0
                amts.append(amt)
        self.ui.cartotal.setText(str(sum(amts)))
and this error
Traceback (most recent call last):
  File "C:/Users/Dennis/PycharmProjects/Savings\expenseswindow.py", line 125, in changecar1
    self.sumcar()
  File "C:/Users/Dennis/PycharmProjects/Savings\expenseswindow.py", line 105, in sumcar
    if child.objectName().endswith.isdigit():
AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit'
I think the syntax is wrong on the "if" statement.  By the way, I am a beginner.
str.endswith() is not the one to use in this case. Use slicing to get last char in the string.

if child.objectName()[-1].isdigit():
thank you