0

[Reading file] in python core, Why "for loop" not work in this practice?

Hi, For the practice I use the code to show result pull up in each day for the code It show result are correct : file = open("/usercode/files/pull_ups.txt") n = int(input()) line = file.readlines() print(line[n]) file.close() But when i used for loop it not work: file = open("/usercode/files/pull_ups.txt") n = int(input()) for line in file: print(line[n]) file.close() What different between readline and for loop when I read each line?

26th Sep 2021, 2:06 PM
Thada Sukprasong
Thada Sukprasong - avatar
5 Réponses
+ 2
Oh I see ... Well, you see, the readlines() returns a `list`, a container that supports item referencing by index. So your first snippet looks like a candidate because it refers an item from the list by a given index <n>. Your second snippet though, you are not referencing n-th row in the file. Why is that so you ask me? because the `for line in file` reads in one line at a time, on each loop round. So the <line> here is a string representing a distinct line in the file, rather than the entire file content splitted into a `list`as readlines() presented earlier. Now let's assume somewhere in the loop that <line> contains "Day 5, 9 pullups\n" And we go like print( line[ n ] ) We expected to see the n-th row, but actually here we are referencing the n-th + 1 character from string <line>. In case <n> was 3, then we see the 4-th character of the string <line> (remember index begins with 0). A possible issue here is when <n> was greater than length of <line> -1, we will get index out of bound error.
26th Sep 2021, 3:58 PM
Ipang
+ 1
I forgot the task requirement, can you briefly describe it for me?
26th Sep 2021, 3:31 PM
Ipang
+ 1
"for line in file:" "file" is an object and not a string or list. You have to write "file.readlines()". Although, I think you forget to write it in your question and maybe your question is actually about the blank lines between the outputs. In this case: Each print() goes to the next line by default. And in each line, there is a "\n" character. So there is one blank line between the output lines. Which leads to failure in the tests. [edit:] Thada Sukprasong i didn't read the problem statement. if it wants just one line, then ignore the second part of my answer.
26th Sep 2021, 3:49 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
0
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). Sample Input 4 Sample Output Day 4, 9 pull ups
26th Sep 2021, 3:38 PM
Thada Sukprasong
Thada Sukprasong - avatar
0
This practice say " recorded each day's results in a new line". So i think the data in the file are the day 1 day 2 . . . I think if it have the separate in new line for newday why i dont use for loop
26th Sep 2021, 3:43 PM
Thada Sukprasong
Thada Sukprasong - avatar