Python Forum
How to exclude characters when counting digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exclude characters when counting digits
#7
Since you were given a lot of advice, just some details about this code
(Jul-02-2018, 05:50 AM)juliabrushett Wrote:
def countDigits(s) :
    digits = 0
    x = len(s)

    for i in range(0, x, 1) :
        ((ord(s[i]) >= 65) and (ord(s[i]) <= 57)) <----- This line does nothing
        digits += 1
        if s.isdigit() == False :
            digits -= 1
            break
        else:   <--- This is parasitic
            continue 
    return digits


s = input("Enter a string\n")

print("There are", countDigits(s), "digits in your string.")
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
RE: How to exclude characters when counting digits - by volcano63 - Jul-02-2018, 07:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,970 Jun-16-2020, 07:22 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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