0

I am trying to write a function that will take in a string and use a dictionary to calculate and return the most common letter in that string. I believe that my code is close to working; however, I get a "cant assign to function call" error on line 5.

Here is my code so far:

def mostCommon(myString):
    charCount = []
    for c in myString.lower():
        if c in charCount:
            charCount(c) += 1
        else:
            charCount(c) = 1
    myVal = 0
    myKey = 0
    for key, value in charCount.lower():
        if value > myVal:
           myVal = value
           myKey = key
        return charCount
Alex Bass
  • 73
  • 1
  • 1
  • 8
  • 2
    Quite a number of problems, the present being the use of `()` instead of `[]`. If you don't want to reinvent the wheel, you can use `collection.Counter`'s `most_common` method to achieve the same thing. – Moses Koledoye Nov 08 '17 at 19:58
  • 1
    you've assigned a *list* to `charCount`. Anyway, you index into a list or dict with `[c]` not with `(c)`, which would *call* the object (hence, the interpreter complaining about you trying to assign to a function call). Also, you then do `charCount.lower()`, neither dict objects nor list objects have a `lower` method. Also, ignoring that, even if you *did* have a `dict` as `charCount`, iterating over it like `for k, v in my_dict:` will give you a `ValueError,` since iterating over a `dict` iterates over the *keys*, and you try to unpack a single value into `k, v`.... – juanpa.arrivillaga Nov 08 '17 at 20:03
  • My advice: while you are still getting a hang of a language, you should do your best to tackle one bit of your program at a time. Your current solution is replete with errors, but you should be testing your code as you write it, so you aren't stacking mistakes on top of each other. – juanpa.arrivillaga Nov 08 '17 at 20:06

4 Answers4

2

Here's your function with the errors corrected.

def mostCommon(myString):
    charCount = {}
    for c in myString.lower():
        if c in charCount:
            charCount[c] += 1
        else:
            charCount[c] = 1
    myVal = 0
    myKey = 0
    for key, value in charCount.items():
        if value > myVal:
           myVal = value
           myKey = key
    return myKey

Here's a much simpler way of doing it

from collections import Counter

def mostCommon(myString):
    return Counter(myString).most_common(1)[0][0]
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • I'm a fan of `dict.setdefault` here. `for c in myString.lower(): charCount.setdefault(c, 0) += 1` – Adam Smith Nov 08 '17 at 20:09
  • 1
    @AdamSmith I'd probably go `defaultdict` myself, but I wanted to preserve as much of their code as possible to make it easier to compare the two and pick out changes – Patrick Haugh Nov 08 '17 at 20:11
  • This code does work now however the output is not whats expected. I should see >>>text = "Beware the mighty mummy" >>>answer =mostCommon(text) >>>print(answer). The answer would be m – Alex Bass Nov 08 '17 at 20:13
0

Well you defined charCount as a list, and then tried to call it like a function. If you want charCount to just be a number, just set it to 0 before your for loop.

Or to use a dict

charCount = {}
for c in myString.lower():
    if c in charCount:
        charCount[c] += 1
SuperStew
  • 2,857
  • 2
  • 15
  • 27
0

I think you intended charCount to be a dict not a list. Here is a simple solution using the max function:

def mostCommon2(myString):
    charCount = {}
    for c in myString.lower():
        if c in charCount:
            charCount[c] += 1
        else:
            charCount[c] = 1
    return max(charCount, key=charCount.get)
0

here after a few things that can help.

  1. The correct syntax to declare a dictionary is charCount = {}

  2. you cannot create an item with charCount(c), you better do charcount[c] = 'c'

To add element to a dictionnary: Add new keys to a dictionary?

sslloo
  • 521
  • 2
  • 10