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()

New Perspectives on HTML5, CSS3, and JavaScript
6th Edition
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Patrick M. Carey
Chapter3: Designing A Page Layout: Creating A Website For A Chocolatier
Section3.2: Visual Overview: Page Layout Grids
Problem 6QC
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
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning