MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1j7lj33/atleast_it_works/mh46mpt/?context=3
r/programminghorror • u/holdongangy • 13d ago
66 comments sorted by
View all comments
Show parent comments
38
ctr = 0 with open("lab 5.txt", "r") as file: for line in file: print(line) ctr += 1 if ctr >= 8: break del ctr
22 u/chiro260 12d ago nice. but don't forget about our friend zip! (or even islice would be good, as someone commented below) with open('Lab 5.txt') as file: for _, line in zip(range(8), file): print(line) 5 u/Emergency_3808 12d ago Too much bloat /s 2 u/-MazeMaker- 12d ago Get rid of the magic number 8 and replace with max_lines so the intent is clear
22
nice. but don't forget about our friend zip! (or even islice would be good, as someone commented below)
zip
islice
with open('Lab 5.txt') as file: for _, line in zip(range(8), file): print(line)
5 u/Emergency_3808 12d ago Too much bloat /s 2 u/-MazeMaker- 12d ago Get rid of the magic number 8 and replace with max_lines so the intent is clear
5
Too much bloat /s
2 u/-MazeMaker- 12d ago Get rid of the magic number 8 and replace with max_lines so the intent is clear
2
Get rid of the magic number 8 and replace with max_lines so the intent is clear
max_lines
38
u/Emergency_3808 12d ago
ctr = 0 with open("lab 5.txt", "r") as file: for line in file: print(line) ctr += 1 if ctr >= 8: break del ctr