+ 2
file = open("/usercode/files/pull_ups.txt") n = int(input()) work = file.readlines(n) print(work) #your code goes here
Pls I need help outputting the 'nth' term
14 ответов
+ 12
work = file.readlines()
print(work[n])
+ 3
And don't forget to close the file.
+ 3
I would suggest to use context manager:
with open("file.txt") as txt:
index = int(input())
lines = txt.readlines()
print(lines[index])
+ 2
Thank you Simba, it works. Am grateful 🙏
+ 2
Prof. Einstein, my sincere apologies I could not get back in time as I have been previously occupied. And thank you for the recognition.
+ 2
Simba Not being pedantic, but I need to emphasize on the extra trailing new-line character (although it doesn't really lead to any issues). The modified code:
work = file.readlines()
print(work[n][:-1])
+ 2
Oh, alright. Thank you Quoi Runtime
+ 1
What am I doing wrong?
+ 1
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
+ 1
That's the problem 👆, thank you 🙏
+ 1
Thank you Kamil Hamid also for your effort 🙏
+ 1
It's no problem Kamil Hamid, I understand
0
What is it doing that you don’t want (as I don’t have access to that file)
0
Is it also possible you could put what this code outputs (with the bug)