Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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()

Transcribed Image Text:Hi bye None sun Dad apple Road school Cold flower Yes no
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
Check Mark
Knowledge Booster
Background pattern image
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
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education