0

Python readlines or file usage

Been really frustrated with this pro problem. everything I do doesn’t work, I’ve tried readlines like it suggests but the output format needs to be: Day 1, 8 pull ups and it returns [Day 1, 8 pull ups \n] so it’s more of a hassle to remove that text. A little annoyed they suggest using readlines but won’t accept the output text it formats to.. I guess I’m just annoyed how everything for pro problems has input in it but they don’t teach how to use input in most of the lessons, gets me discouraged that I can’t code. Tom has done pull ups every day and recorded his results. He recorded each day's results in a new line, so that each line represents each day he has done pull ups. Create a program that takes n number as input and outputs the n-th days result (starting from 0). file = open("/usercode/files/pull_ups.txt") n = int(input()) #your code goes here. My code below doesn’t make sense print(file.readlines(n)) file.close()

1st Feb 2021, 3:30 AM
Steve Barone
Steve Barone - avatar
2 odpowiedzi
+ 4
file.readlines() returns a list, and list can be indexed. Instead of placing the index as the parameter of file.readlines, use it as an index to get the "days" from the list (line each file). x = file.readlines() print(x[n]) x ---> list every line of file n ---> Input - index, represents days
1st Feb 2021, 5:38 AM
noteve
noteve - avatar
+ 1
that solved it thank you! i definitely didnt think of using readlines as a function. ill have to keep that in mind more for some of the stuff!
2nd Feb 2021, 3:23 AM
Steve Barone
Steve Barone - avatar