+ 1

Python Course Module Project - HELP!

For the problem below, I keep thinking I’m getting close by using a for statement to iterate through the lines of the file. I think I need to get the program to return two variables (first letter of the current list item and the character count of the current list item) then concatenate the two variables. But I can’t figure out how to return just the first letter of the given list item or the character count of the given list item. Any help greatly appreciated! Here is the problem: You have been asked to make a special book categorization program, which assigns each book a special code based on its title. The code is equal to the first letter of the book, followed by the number of characters in the title. For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space). You are provided a books.txt file, which includes the book titles, each one written on a separate line. Read the title one by one and output the code for each book on a separate line. For example, if the books.txt file contains: Some book Another book Your program should output: S9 A12

9th Jan 2021, 4:53 PM
Blake Kinsey
9 ответов
+ 2
Blake Kinsey (1) You dont need the incremention of variable 'i' since we are using for loop instead of while loop (2) \n length is 1, not 2. And even if we subtract it to each line, the last line will be wrong since it has no \n (3) We can use string.replace(old, new) method to remove the \n # Here's your original code (modified) https://code.sololearn.com/cs9I0iuh0qPo/?ref=app
9th Jan 2021, 5:29 PM
noteve
noteve - avatar
+ 2
lines=file.read().splitlines() for line in lines: c=str(len(line)) print(line[0]+c) file.close()
9th Jan 2021, 5:32 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 2
《 Nicko12 》 it worked! So, just to make sure I understand: for x in file: x = x.replace("\n", "") print(x[0] + str(len(x))) file.close() 1) remove \n so our character count isnt muddied by it 2) print the character in index “0” (the first character of the string) 3) concatenate first character of the string with the length of the string 4) repeat for each item in the list (each line of the file)
9th Jan 2021, 5:39 PM
Blake Kinsey
+ 1
Please show us your effort and attempt or your readymade code so we may help you. Thanks!
9th Jan 2021, 4:55 PM
noteve
noteve - avatar
+ 1
file = open("/usercode/files/books.txt", "r") #set variable i to represent iteration number i = 0 for x in file: #set variable y to represent the number of characters in the current index's string and use i to set which item in the list to read. y=len(file.readlines()[i]) #subtract two from the character count to remove the "\n" print(y-2) #iterate loop i += 1 file.close()
9th Jan 2021, 5:19 PM
Blake Kinsey
+ 1
thats what i have so far, just trying to get the character count part figured out first, but its returning one line: “15”
9th Jan 2021, 5:20 PM
Blake Kinsey
+ 1
i did just try this and successfully got the character count, except for the last line, like you said: file = open("/usercode/files/books.txt", "r") for line in file: print(len(line)-1) file.close() ill try your suggestions, thanks!
9th Jan 2021, 5:33 PM
Blake Kinsey
+ 1
Blake Kinsey Glad it was solved, and glad you understand. 🙂👍
9th Jan 2021, 5:41 PM
noteve
noteve - avatar