Hello! I am writing a code using Python  but I am having issues with the while loop. It seems repetitive too.  I am trying to open a file, and I want to read each line of that file until last line. Each time I read a line I want to split that string into a list and assing the position 0 of the list to variable initialWord and the position 1 of the list to variable endWord and then print it. The file I am opening just has lines with two words each line. When I call my function I get an error. I also want to clean the code because it seems repetive but If I get rid of the top part outside the while loop I noticed the first line in the file was not being printed.. the loop starts from second line.  I am attaching a picture of the output I want and the error that it shows and picture of how the file forTest.txt looks like.. actually the code is supossed to work with any file that is similar to the one I am using there is no way to know how many lines the file will have..  Here is my code: def pairOfWords():     pairOfWords = open("forTest.txt", "r")     lineWithWords = pairOfWords.readline()     L= lineWithWords.split()     initialWord= L[0]     endWord= L[1]     print("Initial:"+ initialWord , "end:" + endWord)     while lineWithWords:         lineWithWords= pairOfWords.readline()         L= lineWithWords.split()         initialWord= L[0]         endWord= L[1]         print("Initial:"+ initialWord , "end:" + endWord)                   pairOfWords()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello! I am writing a code using Python  but I am having issues with the while loop. It seems repetitive too. 

I am trying to open a file, and I want to read each line of that file until last line. Each time I read a line I want to split that string into a list and assing the position 0 of the list to variable initialWord and the position 1 of the list to variable endWord and then print it. The file I am opening just has lines with two words each line. When I call my function I get an error. I also want to clean the code because it seems repetive but If I get rid of the top part outside the while loop I noticed the first line in the file was not being printed.. the loop starts from second line. 

I am attaching a picture of the output I want and the error that it shows and picture of how the file forTest.txt looks like.. actually the code is supossed to work with any file that is similar to the one I am using there is no way to know how many lines the file will have.. 

Here is my code:

def pairOfWords():
    pairOfWords = open("forTest.txt", "r")
    lineWithWords = pairOfWords.readline()
    L= lineWithWords.split()
    initialWord= L[0]
    endWord= L[1]
    print("Initial:"+ initialWord , "end:" + endWord)

    while lineWithWords:
        lineWithWords= pairOfWords.readline()
        L= lineWithWords.split()
        initialWord= L[0]
        endWord= L[1]
        print("Initial:"+ initialWord , "end:" + endWord)
        
        

pairOfWords()

 

Hi bye
None sun
Dad apple
Road school
Cold flower
Yes no
Transcribed Image Text:Hi bye None sun Dad apple Road school Cold flower Yes no
Initial: Hi end: bye
Initial: None end: sun
Initial: Dad end: apple
Initial: Road end: school
Initial: Cold end: flower
Initial: Yes end: no
Traceback (most recent call last):
File "/Users/ivannarangel/Documents/hw1_3400.py",
File
"/Users/ivannarangel/Documents/hw1_3400.py",
initialWord= L[0]
IndexError: list index out of range
pairOfWords ()
line 34, in <module>
line 28, in pairOfWords
Transcribed Image Text:Initial: Hi end: bye Initial: None end: sun Initial: Dad end: apple Initial: Road end: school Initial: Cold end: flower Initial: Yes end: no Traceback (most recent call last): File "/Users/ivannarangel/Documents/hw1_3400.py", File "/Users/ivannarangel/Documents/hw1_3400.py", initialWord= L[0] IndexError: list index out of range pairOfWords () line 34, in <module> line 28, in pairOfWords
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education