+ 1

Can anyone help me? Please i got an error even if my code is right.

with open("/usercode/files/books.txt") as f: j=1 for line in f: print("Line" +str(j)+": "+str(len(line.split("")))+" words") j+=1

13th Aug 2023, 6:36 AM
Rolando Abareta
Rolando Abareta - avatar
4 Respuestas
+ 8
Rolando Abareta According your code you are use empty string being passed as the separator in the `split()` method,this is wrong.. `split()` method requires a non-empty separator to split the string into a list of words. To fix the error:- use this `" "`instead of `""`.. see this correct version.. https://code.sololearn.com/clTMftWV2FKT/?ref=app
13th Aug 2023, 7:01 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 6
1. you may not make a linebreak within a line of code in python until you mask the linebreak with a backslash. 2. the indentation of the line "j+=1" is wrong. 3. split() may not have an empty string as separator
13th Aug 2023, 6:57 AM
Jan Markus
+ 6
Rolando Abareta , > instead of using an index that has to be *explicitely incremented*, we can use *enumerate()*, starting with value 1. > instead of juggling with spaces and concatenation, it is easier to use comma notation in print() function. to output *int or other numbers*, we do not need to convert them to strings for output when using comma notation. > split() does not need any argument, since it uses spaces as separator by default. https://code.sololearn.com/c6j1hwa7TeO7/?ref=app
13th Aug 2023, 3:57 PM
Lothar
Lothar - avatar
0
OMG Thanks it helps me a lot.
13th Aug 2023, 7:11 AM
Rolando Abareta
Rolando Abareta - avatar