+ 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

1st Jan 2022, 1:15 PM
Abiye Iniabere
Abiye Iniabere - avatar
14 Answers
+ 12
work = file.readlines() print(work[n])
1st Jan 2022, 2:39 PM
Simba
Simba - avatar
+ 3
And don't forget to close the file.
1st Jan 2022, 3:34 PM
Solo
Solo - avatar
+ 3
I would suggest to use context manager: with open("file.txt") as txt: index = int(input()) lines = txt.readlines() print(lines[index])
2nd Jan 2022, 11:41 PM
Rizo
+ 2
Thank you Simba, it works. Am grateful 🙏
1st Jan 2022, 2:51 PM
Abiye Iniabere
Abiye Iniabere - avatar
+ 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.
1st Jan 2022, 6:42 PM
Kamil Hamid
Kamil Hamid - avatar
+ 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])
2nd Jan 2022, 5:40 PM
ƒ ă…€
ƒ ă…€ - avatar
+ 2
Oh, alright. Thank you Quoi Runtime
2nd Jan 2022, 6:05 PM
Abiye Iniabere
Abiye Iniabere - avatar
+ 1
What am I doing wrong?
1st Jan 2022, 1:15 PM
Abiye Iniabere
Abiye Iniabere - avatar
+ 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
1st Jan 2022, 1:48 PM
Abiye Iniabere
Abiye Iniabere - avatar
+ 1
That's the problem 👆, thank you 🙏
1st Jan 2022, 1:49 PM
Abiye Iniabere
Abiye Iniabere - avatar
+ 1
Thank you Kamil Hamid also for your effort 🙏
1st Jan 2022, 2:51 PM
Abiye Iniabere
Abiye Iniabere - avatar
+ 1
It's no problem Kamil Hamid, I understand
2nd Jan 2022, 1:43 PM
Abiye Iniabere
Abiye Iniabere - avatar
0
What is it doing that you don’t want (as I don’t have access to that file)
1st Jan 2022, 1:43 PM
Kamil Hamid
Kamil Hamid - avatar
0
Is it also possible you could put what this code outputs (with the bug)
1st Jan 2022, 2:08 PM
Kamil Hamid
Kamil Hamid - avatar