+ 1
Question about book titles problem python
Basically you get input of lines of book titles and you have to change each book title to first letter+number of letters ##### start of code file = open("/usercode/files/books.txt", "r") #your code goes here books = [book.strip() for book in file] #creating books list which strips each book title of newline characters #function to change book titles def book_coder (books): for book in books: book = book[0]+str(len(book)) return book print (book_coder(books)) file.close() #####end of code I only get the output for the first book but I want the function to iterate over all the list elements. I'm not sure what's missing here.
2 Answers
+ 3
return will come out of function. Instead of using function. Just write without function and print book. That solves problem.
If you know about yeild function, replace yield with return. I think it works also, continues loop
+ 1
Thanks it worked!