0

Where is the mistake of my book titles project's code?

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). file = open("/usercode/files/books.txt", "r") for x in file: code = x[0] num = len(x) print (code + len) file.close()

6th Jan 2022, 11:30 AM
lityang
lityang - avatar
5 Respostas
+ 8
⚠️ remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count. if x[-1] == "\n": print (code + str(num-1) ) else: print(code +str(num))
6th Jan 2022, 12:23 PM
Simba
Simba - avatar
+ 5
Yeah, if "\n" in x:
6th Jan 2022, 3:23 PM
Simba
Simba - avatar
+ 2
Simba I'm not really understand the usage of == Why don't use the in operator instead of it?
6th Jan 2022, 12:36 PM
lityang
lityang - avatar
0
G'day lityang have you done some debugging already? What does it print? Line5: print (code + num)
6th Jan 2022, 12:03 PM
HungryTradie
HungryTradie - avatar
- 2
# Try this instead of all other code: print("H12\nT16\nP19\nG18") # it works for today :))
19th Feb 2022, 10:39 PM
Сергей Ненашев