Question: Python Debugging _____>>>>>>> Help python script that needs to be debugged. Your challenge is to fix the code so that it runs correctly and generates
Python Debugging _____>>>>>>> Help
python script that needs to be debugged. Your challenge is to fix the code so that it runs correctly and generates the correct output. The program itself can tell you when it's working, you just need to correct the problems throughout the code. Once the errors are removed, the code will generate a text file that is compared to a baseline. If they match, the program reports "True" after a successful run.
# This code doesn't do anything meaningful; # Just find and remove the bugs to make the final result True # -------------------------------------------------------
def matches(): with open('output.txt', 'r') as file1: with open('baseline.txt', 'r') as file2:
line1, line2 = file1.readline(), file2.readline()
while line1 and line2: if line1 != line2: return False line1, line = file1.readline(), file2.readline() return True
def aGenerator(aLimit): i = 0 while i < aLimit yield i l += 1
def doSomethingElse(anOutputFile): # emit text to the output file... anOutputFile.write("doSomethingElse ")
theGen = aGenerator(10)
anOutputFile.write("word %s " % theGen.next()) anOutputFile.write("word %s " % theGen.next()) anOutputFile.write("word %s " % theGen.next()) anOutputFile.write("words %s " % "blah" * theGen.next())
return True
def doSomething(anOutputFile): anOutputFile.write("doSomething ") return doSomethingElse(anOutputfile)
# -------------------------------------------------------
if __name__ == "__main__": #This code runs the self-test. This code works fine, so you can ignore it... with open('output.txt', 'w') as theOutputFile: doSomething(theOutputFile) print("matches ", matches())
output is:
doSomething doSomethingElse word 0 word 1 word 2 words blah words blah words blah
baseline is:
doSomething doSomethingElse word 0 word 1 word 2 words blah words blah words blah
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
