Posts: 14
Threads: 9
Joined: Jun 2018
Hello
I am trying to write a program that will count only numeric digits within a string, excluding all letters. What I have currently counts EVERYTHING. How do I fix this?
def countDigits(digits) :
digits = 0
x = len(s)
for i in range(0, x, 1) :
((ord(s[i]) >= 65) and (ord(s[i]) <= 57))
digits += 1
return digits
s = input("Enter a string\n")
print("There are", countDigits(s), "digits in your string.")
Posts: 1,150
Threads: 42
Joined: Sep 2016
Try to check if string (character) is a digit with string's isdigit() method.
Posts: 14
Threads: 9
Joined: Jun 2018
I tried that, but I am returning 0 now (because isdigit() looks for a strings with ALL characters).. How can I fix this?
def countDigits(s) :
digits = 0
x = len(s)
for i in range(0, x, 1) :
((ord(s[i]) >= 65) and (ord(s[i]) <= 57))
digits += 1
if s.isdigit() == False :
digits -= 1
break
else:
continue
return digits
s = input("Enter a string\n")
print("There are", countDigits(s), "digits in your string.")
Posts: 231
Threads: 2
Joined: Sep 2017
Jul-02-2018, 11:34 AM
(This post was last modified: Jul-02-2018, 11:34 AM by gruntfutuk.)
As you are dealing with a string, you can iterate through it with just:
for character in s: character takes each character in turn in the loop.
As isdigit() returns True or False, you only need:
if character.isdigit():
digit += 1 inside the loop.
For future information, you could replace it all with just a return line:
return sum(c.isdigit() for c in s)
I am trying to help you, really, even if it doesn't always seem that way
Posts: 8,089
Threads: 154
Joined: Sep 2016
more efficient approach
import string
def smart_count_digits(my_string):
orig_length = len(my_string)
for digit in string.digits:
my_string = my_string.replace(digit, '')
return orig_length - len(my_string)
Posts: 529
Threads: 0
Joined: Feb 2018
Jul-02-2018, 05:32 PM
(This post was last modified: Jul-02-2018, 05:32 PM by woooee.)
You can also use the string version of a digit (there is no such thing as "only numeric digits within a string", they are all strings) def countDigits(digits) :
digits = 0
for ch in digits :
## assumes zero is not a digit
## and there are no negative numbers
if "1" <= ch <= "9":
digits += 1
return digits
s = input("Enter a string\n")
print("There are", countDigits(s), "digits in your string.")
Posts: 566
Threads: 10
Joined: Apr 2017
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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Jul-02-2018, 05:01 AM)juliabrushett Wrote: ((ord(s[i]) >= 65) and (ord(s[i]) <= 57))
digits += 1
What do you think that does? There's no if there, so counting every character in the string as a digit.
Posts: 566
Threads: 10
Joined: Apr 2017
Couple of more things
- you never compare Boolean to
True or False - because the result will be ... Boolean. For negating Boolean, use operator not
- You may check that a value lies between to boundaries in an easier way
57 <= ord(c) <= 65
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.
Posts: 231
Threads: 2
Joined: Sep 2017
(Jul-02-2018, 12:16 PM)buran Wrote: more efficient approach
import string
def smart_count_digits(my_string):
orig_length = len(my_string)
for digit in string.digits:
my_string = my_string.replace(digit, '')
return orig_length - len(my_string)
Just checking, as I thought my preceding:
def count_func(s):
return sum(c.isdigit() for c in s) would be pretty efficient, but I didn't run time it.
I am trying to help you, really, even if it doesn't always seem that way
|