Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: must be str, not list in Python 3 when calling a function

I created a function to count a letter for example letter e. My function looks something similar to this:

def count_letter(sentence, accents, case):

    lower_case_e = ['e']
    upper_case_E = ['E']
    accent_lower_case = ['é', 'ê', 'è']
    accent_upper_case = ['É', 'Ê', 'È']

    for character in sentence:#If statement for optional argument where ignore_accents == True and ignore_case == False.
        #This loop will count lower case and upper case e as differente letters but will treat accented characters the same.

        if accents == True and case == False:
            lower_case_count = sentence.count(lower_case_e)
            accent_lower_case_count = sentence.count(accent_lower_case)
            upper_case_count = sentence.count(upper_case_E)
            accent_upper_case_count = sentence.count(accent_upper_case)

            total_e_count = lower_case_count + accent_lower_case_count
            total_E_count = upper_case_count + accent_upper_case_count

            return {'Total number of lower case e in sentence ignoring accents':total_e_count, 'Total number of upper case E in sentence ignoring accents':total_E_count }

The point of this function is to count the letter e and depending if it is lower or upper case or if it has accents, to sum the letters together. I created a text file called sentence.txt and it looks like this:

Testing if function can count letter e or E.

I have read the file using the following code:

# Reading the data from sentence.txt as a string
with open('sentence.txt', 'r') as Sentence_file:
    Sentence_string=Sentence_file.read().replace('\n', '')

And after reading the file I try to call the function in the following way:

count_letter(sentence, True, False)

However when I try to run this I get the following error:

TypeError: must be str, not list

Anyone have any idea of what could be going wrong? Could the error be in the way I am reading my txt.file? Any suggestions would be greatly appreciated!

Full error trace looks like this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-b171590ddd67> in <module>()
     29 with open('Test_Sentence1.txt', 'r') as Sentence1_file:
     30     Sentence1=Sentence1_file.read().replace('\n', '')
---> 31 count_letter_e(Sentence1, True, False)
     32 

<ipython-input-2-b171590ddd67> in count_letter_e(sentence, accents, case)
     18         if accents == True and case == False:#If statement for optional argument where ignore_accents == True and ignore_case == False.
     19             #This loop will count lower case and upper case e as differente letters but will treat accented characters the same.
---> 20             lower_case_count = sentence.count(lower_case_e)#counting lower case e with no accent from the sentence
     21             accent_lower_case_count = sentence.count(accent_lower_case)#counting lower case e with accents from the sentence
     22             upper_case_count = sentence.count(upper_case_E)#counting upper case E with no accent from the sentence

TypeError: must be str, not list
like image 911
adda.fuentes Avatar asked Oct 20 '17 21:10

adda.fuentes


2 Answers

The "count()" function only accepts a string as input. For example:

lower_case_count = 0
for lower_case_e_char in lower_case_e:
    lower_case_count += sentence.count(lower_case_e_char)
print(lower_case_count)
like image 171
Forrest Abouelnasr Avatar answered Sep 19 '22 03:09

Forrest Abouelnasr


In the line indicated in the stack trace, you are calling the .count() method and passing in the variable lower_case_e which has a value of ['e']. The .count() method expects a string to count, not multiple values. Example:

>>> testing = 'aba'
>>> testing.count('a')
2
>>> testing.count(['a'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a string or other character buffer object

So, for the values you want to count that have multiple characters (such as accent lower case), you will need to to loop through and add up the value of count() for each string method, not the entire list at one time.

like image 27
MrName Avatar answered Sep 20 '22 03:09

MrName