- 1

Hello world what is the problem with this code if the book is Harry Potter it should output H12 with 12 the lenght

File=open("book. Txt, r) A=file.split() For i in A: X=i[0]+"len(i)" Print(X)

9th Mar 2021, 9:18 PM
Houssem Kitar
Houssem Kitar - avatar
4 Réponses
+ 3
you should first respect case of keywords and variable names: 'File' and 'file' are not the same variables, and 'For' is not 'for'... to convert number to string you should use: str(number) so, your corrected code would be: File=open('book.txt','r') A=File.read().split() for i in A: X=i[0]+str(len(i)) Print(X) obviously, you could (better practice) lowercase the first letter of variables, and reserve capitalization to class names ;)
9th Mar 2021, 9:53 PM
visph
visph - avatar
+ 1
open get two string arguments, wich should be properly enclosed in quotes (either single, double, or even triple -- docstring). also, default char for split is comma (,), but the file provide each book on one line, so you better would do split('\n'), and you must read file before: file = open('book.txt','r') A = file.read().split('\n')
9th Mar 2021, 9:58 PM
visph
visph - avatar
0
If you want to print out ’H12’, you can look at this example: https://code.sololearn.com/cBk8WxGxO21f/?ref=app
9th Mar 2021, 10:06 PM
Per Bratthammar
Per Bratthammar - avatar
- 1
Thanks
10th Mar 2021, 8:10 AM
Houssem Kitar
Houssem Kitar - avatar