Can someone please help me fix the error in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone please help me fix the error in this code?

I'm trying to create a program to calculate the length of the last word. https://code.sololearn.com/cA7A9A720A1A/?ref=app

13th May 2021, 10:37 PM
Maxwell D. Dorliea
Maxwell D. Dorliea - avatar
6 Answers
+ 5
Just a couple minor changes. I moved input into the main code. The removed the extra declarations in your object. Done. class Solution: def lengthOfLastWord(self, s: str) -> int: lists = s.split() str=lists[-1] print(len(str)) r=Solution() r.lengthOfLastWord(input())
13th May 2021, 11:14 PM
Jerry Hobby
Jerry Hobby - avatar
+ 5
You could always just use print(len(input().split()[-1])) 🙂
14th May 2021, 3:25 PM
David Ashton
David Ashton - avatar
+ 3
Thanks alot
14th May 2021, 4:10 PM
Maxwell D. Dorliea
Maxwell D. Dorliea - avatar
+ 2
Calvin Thomas thanks alot
14th May 2021, 10:01 PM
Maxwell D. Dorliea
Maxwell D. Dorliea - avatar
+ 1
Thanks alot
13th May 2021, 11:15 PM
Maxwell D. Dorliea
Maxwell D. Dorliea - avatar
+ 1
Maxwell D. Dorliea Here's another solution: a = input() print(len(a) - a.rindex(" ")) # Hope this helps
14th May 2021, 7:42 PM
Calvin Thomas
Calvin Thomas - avatar