- 1

I want this program to show me my lines one by one, it shows me the first line yet doesn't continue. I don't understand the prob

https://code.sololearn.com/c38tC326GpnB/?ref=app

9th Aug 2017, 1:32 PM
Bob
5 Answers
0
try to store the file.read.splitlines in a variable and take only lines ito print, like: var a = file.read.splitlines and later in printf call just do a[i]
9th Aug 2017, 2:04 PM
Paul
+ 5
I don't know what was your initial not working code, as you've edited it... but: > I think your main mistake is to compare user entry (x) with 1 rather than '1' (number instead of string)... > What I would do would be: i=0 # using 'with' statement the stream (file here) is implicitly close at end of block with open('ex15_sample.txt') as file: for line in file: print(line) x=input("please press 1 to continue \n" ) if x=="1" and i < 3: i += 1 else: print("goodbye") break
9th Aug 2017, 4:30 PM
visph
visph - avatar
- 1
thank you so much paul it work! Though may i ask why it didn't work before? what did storing file.read.splitlines to a help in?
9th Aug 2017, 2:39 PM
Bob
- 1
i may be 60% wrong cause i dont know how exacly file.read works, but i assumed that: a) the file.read have to open a file so it does it and it is kept open. in the next loop run it pretrnds to open file once more but it is hold by another process, or b) it opens file once and read all lines and in the next loop it already rode all lines so whats left? i,m keen to version a) tho
9th Aug 2017, 2:44 PM
Paul
- 1
glad it helped tho!
9th Aug 2017, 2:44 PM
Paul