2

I am asked to create a function in which it counts all the leters from b-s within a string. Here is what i have so far:

def strange_count(s):
    count = 0
    s = s.lower()
    s_count = [b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s]
    while count <= len(s):
        for i in range(len(s_count)):
            if s_count[i] == s[count]:
                count += 1
    return      

It is returning an error, any help would be greatly appreciated

  File "", line 4, in strange_count
    s_count = [b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s]
NameError: name 'b' is not defined
1
  • Do you need to worry about upper case letters? What about foreign-language letters with diacritics like "é"? Commented Oct 10, 2015 at 19:49

6 Answers 6

2

This line:

s_count = [b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s]

should be like this:

s_count = ["b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"]

Then you'll find that you're in an infinite loop, so try rewriting like this:

def strange_count(s):
  count   = 0
  s       = s.lower()
  s_count = ["b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"]
  for letter in s:
    if letter in s_count:
      count += 1
  return count

print(strange_count("asfdsjfdlkwjrwoiureaoifhwabrejwer"))
Sign up to request clarification or add additional context in comments.

Comments

1

Rather than making a list, you can use the fact that letters sort lexicographically so you can compare them with > and <

def strange_count(s):
    total = 0
    for letter in s:
        if letter >= 'b' and letter <= 's':
            total += 1
    return total

For example

>>> strange_count('dictionary')
7

Or more compactly using a generator expression

def strange_count(s):
    return sum(1 for i in s if i >= 'b' and i <= 's')

Comments

1

The error indicates that you're using undefined variables - b, c, etc. It seems you meant to use those characters, in which case you should surround them in quotes:

s_count = ['b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s']

Having said that, it's worth remembering that characters are ordinal, so you could just use the <= and >= operators. Additionally, IMHO, defining a function for this seems like a bit of an overkill when you can just use sum and a list comperehension:

mystring = 'some string'
strange_count = sum(1 for c in s if c >= 'b' and c <= 's')

Comments

1

The Problem - Variables vs Letters

s_count = [b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s]

The above is not declaring a list with elements that corresponds to the range of letters you are talking about.

What you are really saying is that s_count should be a list where the elements shall have values corresponding to variables named b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, and s.

You get a diagnostic (error message) from the python interpreter because none of these variables have been declared.


The Solution

If you want to create a list of characters you will need to wrap them in ' or " (as in the below).

s_count = ['b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s']

Note: Given that the above is very tedious to both write and maintain, a more suitable approach for initializing s_count would be s_count = list ("bcdefghijklmnopqrs") — which yields the desired result.

Comments

0

You can in your case as Mureinik's answer see if each char is >= "b" and < ="s", another efficient approach for specifying certain characters would be using a set of the chars you want to check for and summing how many times a char from s is int the set:

def strange_count(s):
    s = s.lower()
    s_count = {"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s"}
    return sum(ch in s_count for ch in s)

As far as your error goes you need to actually create strings using double or single quotes:

["b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s"]

Using b,c... python is going to look for variables defined with the names a, b, c.... so you would get a NameError unless you actually had those variables defined somewhere.

If you were to use <= then you can could use a simplified chained expression "b" <= ch <= "s" with sum:

def strange_count(s):
    return sum("b" <= ch <= "s" for ch in s) 

Comments

0

If you are not dealing with very long strings or you are not requested to take in consideration computational costs you can use something like this:

input = "Hello world"
input = input.lower()
list_chars = ['b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s']
counts = {}
for c in list_chars:
    counts[c] = input.count(c)

print counts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.