+ 4
I'm solving the new test on python and im stuck at book title
Im getting the same expectied result as an output but it still tells me failed task what's wrong???
37 Antworten
+ 2
https://code.sololearn.com/c5LfMwEmIx2v/?ref=app
compare your code with this one👆🏻
+ 13
file = open("/usercode/files/books.txt","r").readlines()
for line in file:
line = line.rstrip("\n")
print(line[0] + str(len(line)))
+ 9
A simpler way with 5 lines of code.
A simple explanation, loop through all lines, and if a line has trailing/ending "\n" remove it from the count of the length.
https://code.sololearn.com/cdaPdMWWeN3W/?ref=app
+ 5
Mustafa Thaer , you have to keep in mind that there’s a special condition for the last book of the list, which is that it has no newline “/n” character unlike the rest of the books. So you have to take that into account, every single book will have an extra countable character (newline or “/n”) except for the last one. My suggestion to you is to insert an if/else statement to address that. Good luck!
+ 2
Simba its only 6 lines 2 of them are given for open and close file the code runs and my result and the expectid result are the exact same .. is it ok to show you my answer??
this is my solution
file = open("/usercode/files/books.txt", "r")
for line in file:
nums= list(line)
x=len(nums)-1
print(line[0]+str(x))
file.close()
+ 2
Avalon text file contains a books names they want me to print the first letter of the name with how many characters it has "space included " with every line has \n need to exclude
For exaple
"Harry potter" = H12 and so on
+ 2
Ren thank you , can you explain why it worked while my code did'nt? Im cunfused really
+ 2
Hi, Mustafa Thaer. Did you manage to solve it? In the instructions, they mention something important: all the lines have this special character '\n' except the last. If only there was a way to count the length of a line without counting a specific character... 😁 Did you know it is possible to remove the white spaces of a string with a certain method? Maybe that works with other characters, too. I hope that helps.
+ 1
which task? there is no explanation what is given and task what you are trying to do
+ 1
try this one:
for line in file:
print(line[0] + str(len(line)-2))
+ 1
Avalon it worked but still not pass
+ 1
Avalon i did some changes and keep geting the same result but did'nt pass 💔
+ 1
Can you post a link to the task?
+ 1
send me your code link and i’ll try to look for the error :)
+ 1
Ren thank you i sent it to your DMs
+ 1
fch0 at the first it was only 3 results showing i did'nt know that there is more under so i was confused cuz the problem was at last line thanks for you help i appreciate it
+ 1
sorry, I’m not able to view my messages in sololearn(-.-;)
+ 1
K. Bharath 😂😁😁😂😂👍 do you also need our bank account password?
+ 1
Well you need to "strip" ☺. Sounds like you already have though