Computer Science, asked by kanchanranno, 2 months ago

3. Write a function display oddLines () to display odd number lines
from the text file. Consider above file – friends.txt.​

Answers

Answered by sg1512313
5

Answer:

sjjsshsshsjjsjsjsjsjsjsshhsjsjsjnssnnsnsns

Answered by vinod04jangid
0

Answer:

def display_oddLines():

   f = open("friends.txt")

   count =0

   for lines in f:

       count += 1

       if count % 2 != 0:

           print(lines)

   f.close()

Explanation:

Inside the function display_oddLines(), the 1st line opens a file named friends.txt which is being stored in a variable f. In 2nd line, we have initialized a variable count to 0. Then we have used a for loop to iterate thought f. Next we have used increment operate to increment count variable after each iteration. Using count variable we filter the odd lines from the file and to do so, we have using the modulo operator which outputs the remainder. So we check if count % 2 is not equal to 0 then that particular line in the file is odd one. Then using print function, we display each line one by one after each iteration. close() function is used to close the file after the required work is done.

#SPJ3

Know more about opening modes of file here:

https://brainly.in/question/14033176?msp_srt_exp=4

https://brainly.in/question/39977005?msp_srt_exp=4

Similar questions