Reputation: 325
I'm trying to learn python. I got to the point where we're learning loops "for" and got a bit of a pickle. 1st assignment was to build a function that will count all spaces, my solution was:
def count_spaces(s):
cnt = 0
for char in s:
if char == " ":
cnt = cnt+1
return cnt
and now I'm trying to build a new function, that can accept string, char and will return the count of the specific char
for example:
print(count_char("Hello world!", " ")
and the screen will show 1 (1 space is found) this is where i got stuck:
def count_char(s, c):
s=[...]
num = 0
for x in s:
if x == x:
num = s.count(c)
return num
it's returning only 0 ....
please help
Upvotes: 0
Views: 372
Reputation: 9
Remove the s=[...]
. What ever the ...
is, it shouldn't be there because s
is an input argument and you do not want to change it.
About the code: It should only be:
count = string.count(substring)
because the str.count()
implements what you want to achieve.
If you want to avoid using the built in methods and implement it yourself then you should replace
num = s.count(c)
with num += 1
and the x == x
(because x will always equal x and you'll be counting all the characters) with x == c
like so:
def count_char(s, c):
num = 0
for x in s:
if x == c:
num += 1
return num
Upvotes: 0
Reputation: 80
Remove the s=[...]
. What ever the ...
is, it shouldn't be there because s
is an input argument and you do not want to change it.
About the code: It should only be:
count = string.count(substring)
because the str.count()
implements what you want to achieve.
If you want to avoid using the built in methods and implement it yourself then you should replace
num = s.count(c)
with num += 1
and the x == x
(because x will always equal x and you'll be counting all the characters) with x == c
like so:
def count_char(s, c):
num = 0
for x in s:
if x == c:
num += 1
return num
Good luck and happy learning!
Upvotes: 0
Reputation: 71434
You're overwriting your s
argument at the start of your function:
s = [...]
which makes the rest impossible to do. Don't do that! :)
If you're allowed to use the count
method (like your code is doing) you don't need the for
loop at all:
def count_char(s: str, c: str) -> int:
"""The number of character c in string s."""
return s.count(c)
If you wanted to do it without using count
, you can write it exactly like your count_space
function, but replace the " "
with the c
parameter:
def count_char(s: str, c: str) -> int:
"""The number of character c in string s."""
cnt = 0
for char in s:
if char == c:
cnt = cnt+1
return cnt
Or you could use a for
comprehension along with the sum
function:
def count_char(s: str, c: str) -> int:
"""The number of character c in string s."""
return sum(1 if char == c else 0 for char in s)
Or you could use a Counter:
from collections import Counter
def count_char(s: str, c: str) -> int:
"""The number of character c in string s."""
return Counter(s)[c]
Upvotes: 1
Reputation: 699
Try this!
def count_spaces(s, c):
cnt = 0
for char in s:
if char == c:
cnt = cnt+1
return cnt
Upvotes: 0