0

Hi can any one help me with a problem that I have learning how to use the archives

I'm doing a test task , the test task is this: Tom has run runs every day and has recorded his results. He recorded the results for each day on a new line, so that each line represents each day that he has done "pull ups." He creates a program that takes the number n and outputs the result of the n-th days (starting from 0). Input example 4 Output example Day 4, 9 pull ups Remember the readlines () function that returns a list containing each line of the file as one element of the list. I don't know how to make that the info that the user put, take the exact line and show it. With a list i know how to do it but I don't know how to use the readlines function in order to use it in the code . The task is a PRO task, if anyone can help me , I'll appreciate it

7th Jul 2021, 8:18 PM
Alan Restrepo
Alan Restrepo - avatar
4 Réponses
+ 1
readlines() returns a list of 'lines' that is 0 based. If the value at the 0 index is "Day 0, 8 pull ups", then it is at the nth index. So, to find "Day 4, 9 pull ups", when 4 is the input N, you would use lines[N] to get the value.
7th Jul 2021, 8:45 PM
ChaoticDawg
ChaoticDawg - avatar
0
Hi, how are you , so how it would look like in the code: file = open("/usercode/files/pull_ups.txt", "r") x=int(input()) for line in file : if x >= 0: print (line) file.close()
7th Jul 2021, 8:54 PM
Alan Restrepo
Alan Restrepo - avatar
0
If you could show me it would help
7th Jul 2021, 9:42 PM
Alan Restrepo
Alan Restrepo - avatar
0
x = int(input()) file = open("/usercode/files/pull_ups.txt", "r") lines = file.readlines() file.close() print (lines[x])
8th Jul 2021, 12:20 AM
visph
visph - avatar