0

Help!!!!!

What is wrong with my code? Project called book titles. file = open("/usercode/files/books.txt", "r") X = file.readlines() Y = len(X) for i in range(Y): A= X[i][0] B= len(A) print(A, str(B))

17th Apr 2021, 12:44 AM
Ailana
Ailana - avatar
8 odpowiedzi
+ 3
1. You don't want A and str(B) to be separated by a space. Use concatenation, f-strings, or the sep='' argument to correct this issue. 2. You don't want the last character (the newline character '\n') for each of the lines in the file other than the last line in the file, which doesn't have it already. Hint: if you loop over all of the lines in the file and just strip() the whitespace from each line you'll get the correct length for the line.
17th Apr 2021, 1:00 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You can concatenate a string in python using the + operator. It shows you this in the course. print(A+str(B)) Or a f-string, which is similar to using the format method. print(f"{A}{B}") Prepend a string with f (for format) then place your object or expressions etc within curly braces and they will be evaluated and converted to the formatted string. Or you can change the print functions seperator value by passing an empty string as the sep value for the print function. print(A, B, sep='') You can use the strip() method on any string just by calling it on that string object and it will remove all whitespace characters from the beginning and end of the string. There is also the lstrip() and lstrip() methods for removing whitespace from the beginning or the end respectively. line = ' Hello world \n' print(line.strip()) # outputs 'Hello world'
17th Apr 2021, 1:17 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You want the length of the current line you're on from the file (X[i]), minus the trailing newline character. Not the length of the files name, or the length of A which would always be 1.
17th Apr 2021, 1:23 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You can also loop over the lines of the file, without using readlines() or read(), like this; for line in file: # code for each line of the file # line[0] # first character of line ...
17th Apr 2021, 1:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Try not using range() you don't need it. Just loop over the lines of the file. Then get the 0th character from the line and the length of the line.strip() and output the values in the loop.
17th Apr 2021, 2:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
What is wrong with my code? Project called book titles. file = open("/usercode/files/books.txt", "r") X = file.readlines() Y = len(X) for i in range(Y): A= X[i][0] B= len(A) print(A, str(B))
23rd Mar 2023, 8:26 AM
Behruz Olimov
Behruz Olimov - avatar
0
Here is my 2nd try: file = open("/usercode/files/books.txt", "r") X = file.readlines() Y = len(X) for i in range(Y): A= X[i][0] B= len('books.txt') print(A,B,sep='') I get H9 ...etc Why???
17th Apr 2021, 1:05 AM
Ailana
Ailana - avatar
0
I tried this. with open("/usercode/files/books.txt","r") as f: c = len(f.readlines()) X = f.readlines() for i in range(c): A = X[i][0] print(A,c,sep='') I don't know what's the problem in my code. Plz explain!
17th Apr 2021, 2:24 AM
Ailana
Ailana - avatar