Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
yee ya ( 1 ) The len will not cause an error but the + will, because the len(i) will return an integer and we cannot concatenate integer and string unless if we convert that integer to string. ---> str(len(i)) ( 2 ) The variable "i" in the "for loop" represents each line of the file content therefore we dont need to read the file again by using "file.read(1)", we can just use the variable "i" instead. To get the first letter of each line: ---> string[0] or i [0] Additional: string.strip() as I said earlier will remove the spaces and "\n" at the end of the string because we only need to count the length of the character of the string exluding the escape character. https://code.sololearn.com/cQtOmn0fxpG1/?ref=app
18th Jan 2021, 3:56 AM
noteve
noteve - avatar
+ 3
yee ya Well.. same here. All the best! and Good luck to both of us!
18th Jan 2021, 4:09 AM
noteve
noteve - avatar
+ 2
Please show us your attempt so far so we may help. Thanks Here are some hints: len ---> length of string You can use file.readlines() to have a list with each line of the file as its each element. Or you can just iterate the file by using for loop. string.strip() removes the "\n" at the end of the string.
18th Jan 2021, 3:37 AM
noteve
noteve - avatar
+ 2
yee ya Actually yes you can do some math, if-else condition to be specific. If "i" variable has "\n" at the end then, "len(i) - 1" Else if the "i" variable is the last line of the file i.e. no "\n", then just "len(i)" This is the common solution I see and this is working properly but just longer. https://code.sololearn.com/cr66jfio7h78/?ref=app
18th Jan 2021, 4:05 AM
noteve
noteve - avatar