+ 2
Python "Book title" project
I'm a noob so i just used print("H12\nT16\n..."). And I'm curious how to complete this project properly
60 odpowiedzi
+ 115
Answer to this project:
file = open("/usercode/files/books.txt", "r")
for line in file:
if line[-1] == "\n":
print(line[0]+str(len(line)-1))
else:
print(line[0]+str(len(line)))
file.close()
1: "file" refers to opening books.txt file in read mode
2: for line (referring to the book title) in the books.txt file
3: if book title contains "\n" at the end
4: print first letter([0] refers to the first item in the list aka book title) of book title + (total number of characters - "\n")
In this case, len is used to calculate the total number of characters in the book titles
5: else = when conditions of if is not met = "\n" is not at the end of the book title
6: print first letter([0] refers to the first item in the list aka book title) of book title + total number of characters (using the function len!!)
7: close books.txt file
+ 12
Aleksei Shevtsov , please show us your try.
To give you a hint, how you can get rid of the problem with the new-line sequence (\n):
- the trailing new-line sequence can be stripped off with the string method strip() or rstrip()
+ 8
file = open("/usercode/files/books.txt", "r")
Book_Titles = file.readlines()
for line in Book_Titles:
if line != str(Book_Titles[-1]):
print(line[0]+str(len(line)-1))
else:
print(line[0]+str(len(line)))
file.close()
~~~~~~~~~~~~~~~~~~~~OR~~~~~~~~~~~~~~~~~~~~~
file = open("/usercode/files/books.txt", "r")
Book_Titles = file.readlines()
for line in Book_Titles:
if line == str(Book_Titles[-1]):
print(line[0]+str(len(line)))
else:
print(line[0]+str(len(line)-1))
file.close()
+ 7
X = file.readlines()
Y = len(X)
For i in range(y): or for i in range(len(x)):
A= x[i][0]
B= len(A)
If i == B-1:
Print(A + str(B))
Else:
Print(A + str(B-1))
+ 7
file = open("/usercode/files/books.txt", "r")
newList = file.readlines()
for line in newList:
if line[-1] == "\n":
print(line[0]+str(len(line)-1))
else:
print(line[0]+str(len(line)))
file.close()
Just making Use of the readlines method
+ 6
the answer for the question is,
a= file.readlines()
for i in a:
print(i[0]+str(len(i.strip())))
file.close()
explaination:
They are asking indirectly that u have to print the 1st letter of the book and the total number of letters in the name of book. In the for loop, i am going the print the first letter of the book name i.e i[0] and I used concatenation. then i am going to print the total numbers of letters, for that at first i am going to find the length of the string without any gaps so i used strip function (i.e) print(i[0]+str(len(i.strip())))
+ 6
Annie Láng ,
the reason why the additional space written to the existing file has no effect, is that '\n' will not be appended at the last line, but it creates a new line that contains just '\n'.
using the replace() method creates a new string as result, so we have to use:
i = i.replace("\n",""), but this is not very efficient.
what we can do is to use the rstrip() (right strip) method, that can remove trailing characters. if we do not give any character as argument for this method, all kind of whitespace is stripped. since the newline sequence '\n' is seen as a whitespace, it will be stripped off.
file = open("/usercode/files/books.txt", "r")
list = file.readlines()
for i in list:
#i.replace("\n","")
print(str(i[0]) + str(len(i.rstrip()))) #[edited]
file.close()
+ 5
Please share your code with us. The best way to do this, it to put it in playground and link it here. Please also give us a description of the task. Thanks!
+ 5
Danijel Milovanov ,
since you have asked for possible optimization of the code, i have put your code in the attached file, also some comments and ideas how the code can be improved.
https://code.sololearn.com/cYmm2IRV8Azw/?ref=app
+ 4
Please make your question more clear
https://www.sololearn.com/discuss/1316935/?ref=app
+ 3
Its a seen bug in sololearn, in these recent days
+ 3
Biologia ,
please do not post a new question in an existing question of another person. you have a much better chance of getting a helpful answer by doing a new post. an other reason is: if the original poster will delete his post, everything will be deleted also your question.
as your question has been asked frequently, please use the search bar to get answers.
BTW: is the file that you are using with pycharm exactly the file used by sololearn? how did you get this file from sololearn? did you re-create if on the windows device?
the reason for this behaviour / difference is that normally all lines in the file (except the last one) will be terminated by a "new-line" sequencee, that will also be counted when using len() function.
+ 2
For line in file:
Print(str(len(line)))
+ 1
It's a very good project
The key is to iterate every single line using the for loop next we put an if condition to the for loop where we check if the last character of the line is '\n', if true we print the first letter + length of the line -1 converted to a string. If false print the first letter + length of the line converted to a string
file = open("/usercode/files/books.txt", "r")
Book_names = file.readlines()
for lines in Book_names:
if line[-1] == "\n":
print (line[0] + str(len(line)-1))
else:
print (line[0] + str(len(line)))
+ 1
file = open("/usercode/files/books.txt", "r")
#your code goes here
books=file.readlines()
for i in books:
if i==books[-1]:
value=len(i)
else:
value=len(i)-1
print(i[0]+str(value))
file.close()
+ 1
the simplest way
file = open("/usercode/files/books.txt", "r")
for book in file:
print(book[0]+str(len(book.strip('\n'))))
file.close()
+ 1
file = open("/usercode/files/books.txt", "r")
#your code goes here
A1=file.readlines()
for line in A1:
if '\n' in line:
print(line[0]+str(len(line)-1))
else:
print(line[0]+str(len(line)))
file.close()
+ 1
A simple answer that can be helpful:
file = open("/usercode/files/books.txt", "r")
list_books = file.readlines() # Get the file lines in a list
for book in list_books:
book = book.replace("\n", "") # replace the \n
print (str(book[0]) + str(len(book)))
file.close()
+ 1
My answer:
file = open("/usercode/files/books.txt", "r")
#your code goes here
for b in file.read().split("\n"):
print(b[0].upper() +str(len(b)))
file.close()
I what i need is to create a list of titles without including "\n" in each item. So i use .split("\n")
You know what to do next
+ 1
file = open("/usercode/files/books.txt", "r")
#your code goes here
for line in file:
count = line.replace("\n", "")
print(line[0] + str(len(count)))
file.close()