I am playing with Codewars to further show me just how much I don’t know LOL.
I’m pretty sure I have the basic structure ok, but I’m getting an error that doesn’t make sense when I’m looking the error up on google:
The task:
You probably know the “like” system from Facebook and other pages. People can “like” blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement the function which takes an array containing the names of people that like an item. It must return the display text as shown in the examples:
[] --> "no one likes this" ["Peter"] --> "Peter likes this" ["Jacob", "Alex"] --> "Jacob and Alex like this" ["Max", "John", "Mark"] --> "Max, John and Mark like this" ["Alex", "Jacob", "Mark", "Max"] --> "Alex, Jacob and 2 others like this"
My code:
def likes(names):
if names == []:
print('no one likes this')
elif len(names) == 1:
print((0), 'likes this')
elif len(names) == 2:
print((0), 'and' (1),'like this')
elif len(names) == 3:
print((0), ',' (1), 'and', (2), 'like this')
elif len(names) > 3:
print((0), ',' (1), 'and', len(names)[2:], 'others like this')
pass
The results:
Test Results:
Basic tests
Log
no one likes this
None should equal ‘no one likes this’
Log
0 likes this
None should equal ‘Peter likes this’
Unexpected exception raised
Traceback (most recent call last):
File “/workspace/default/.venv/lib/python3.10/site-packages/codewars_test/test_framework.py”, line 112, in wrapper
func()
File “/workspace/default/tests.py”, line 8, in _
test.assert_equals(likes([‘Jacob’, ‘Alex’]), ‘Jacob and Alex like this’)
File “/workspace/default/solution.py”, line 7, in likes
print((0), ‘and’ (1),‘like this’)
TypeError: ‘str’ object is not callable
I don’t see a string that could be confused with a variable that might this error.
I’m thinking this is because I’m trying to index a string? Do I have to include the word “index” before the (#)?