0
Could someone please help me with the explanation of the Book Titles challenge in the Python Core course?
I understand the point of reading the files and stuff, but I'm a little bit stuck
6 Antworten
+ 7
Victory Amadi ,
the best would be to share your code with us. please link it here.
if you have not done a try by yourself upto now, please do so and link your code here. this enables us to find out where the issue is exactly. so you can get the best support from the community.
▪︎please also post the complete task specification here. everything you need to know for solving this exercise is written there.
thanks!
+ 3
Pretty simple, really.
You take the first letter of the title of each book (each line), and concatenate that to the length of the title (len(string)). Print the result to the screen.
You can either use the readlines function and loop through it, or use:
for line in file:
#code here
I opted for the second choice, since it's more concise.
+ 1
I think trying your code out side sololearn and printing in and outs of your code might help debugging it
+ 1
This is the question
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
file = open("/usercode/files/books.txt", "r")
#your code goes here
file.read()
print(file.read(0))
print(file.read())
file.close()
Here is the code. I know there should be an error somewhere
+ 1
So you should read it line by line like:
Books = file.read().split("\n")
Then you should get the first of each title and combine them with there length
0
I like how the OP specifically states that they have trouble understanding the question itself (I assume), and then everyone immediately expects code when obviously the poor lad doesn't even have the understanding of it to even begin. Very sad lapse of judgement, I must say, very sad, hence why I took it upon myself to see the challenge he was referring to.
@Victory Amadi, as I stated in my first answer, you have two ways you can solve
this problem. You can:
A. use file.readlines() to return a list containing all the lines within the file, then loop through them with a for loop to print out the codes via:
for line in file.readlines():
#generate the book code for this book
#print the book code for this book
B. Use the more concise for loop that I used in my solution for this same challenge (interestingly it has the same result as using readlines(), I suppose it's the default for passing a file handle to a for loop), ie:
for line in file:
#do the same here
I will leave the exact method of generating the book code for a book to you, since it's fairly easy, it's the first character of the book title added to the length of the characters in the title.