- 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)
4 odpowiedzi
+ 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 ;)
+ 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')
0
If you want to print out ’H12’, you can look at this example:
https://code.sololearn.com/cBk8WxGxO21f/?ref=app
- 1
Thanks