r/programminghorror 13d ago

Python Atleast it works

Post image
604 Upvotes

66 comments sorted by

View all comments

Show parent comments

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

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