Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
syntax for endswith a digit
#1
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.
Reply
#2
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():
Reply
#3
thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 286 Jan-27-2024, 05:23 PM
Last Post: Winfried
  prefix ID Number with 0,00 make 3 digit. mg24 1 708 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  if a string has a digit - print tester_V 2 2,081 Jan-16-2021, 04:48 AM
Last Post: tester_V
  str.endswith Output Query eddywinch82 8 3,721 Aug-21-2020, 11:59 PM
Last Post: eddywinch82
  Is this right code for 3 digit pin ? fatjuicypython 1 1,785 Aug-21-2020, 11:15 PM
Last Post: bowlofred
  Writing a code with 3 digit numbers while they cant contain 0 and then sum them AFKManager 2 2,341 Jan-13-2019, 12:00 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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