TypeError: must be str, not list in Python 3 when calling a function
TypeError: must be str, not list in Python 3 when calling a function
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)
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.
TypeError: must be str, not list in Python 3 when calling a function
You sentence.count()
expect a string, not a list. Change your lower case, upper case etc to strings. For example, lower_case_e = e
instead of [e]
. It should resolve your error. For the rest, you need to improve your function in many other ways.
you can try something like:
lower_case_e = e
lower_case_count = sentence.count(lower_case_e)
accent_lower_case_count = [sentence.count(item) for item in accent_lower_case]
total_e_count = lower_case_count + sum(accent_lower_case_count)
You can see this from error message. Look at line 31 and then 20, you can see that something is wrong with sentence.count(). Then inspect the error message which tells you that something went wrong because a list was seen by the function while it was expecting a string.
These pointers should get you going.