Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are 30 locations in the code marked with \"Questions n\" that require you

ID: 3908614 • Letter: T

Question

There are 30 locations in the code marked with "Questions n" that require you to provide code comments. Comments should:

- describe what the code just below the comment does

- describe program execution flow

- describe changes in values to variables

Version of Python: 3.6.5

Thanks in advance!!! Assume indents on code where necessary.

File:

'''
For each Question, provide a series of detailed comments below
describing what the code does. Use as much space as you need.

The first question appears in the main function. There are 30 questions
that require you to provide detailed comments explaining what the line or
block of code does.
'''

import random
import os

# Question No. 8
class File:
# Question No. 9
def __init__(self,fileName):
self.name = fileName
self.handle = open(self.name,'a')

# Question No. 10   
def addLines(self,text):
self.handle.write(text + ' ')

# Question No. 11
def addRandomText(self,numberOfLines):
for i in range(numberOfLines):
self.handle.write('Som Random Text ')

# Question No. 12
def deleteFile(self):
os.remove(self.name)

# Question No. 13
def displayFileContents(self):
for line in self.name:
print(line)

# Question No. 14
def closeFile(self):
self.handle.close()

# Question No. 15
def processFiles(files):
# Question No. 16
try:
# Question No. 17
theFiles = []
# Question No. 18
for j,k in files.items():
# Question No. 19
theFiles.append(File(k))
# Question No. 20   
return theFiles
except:
return -1

# Question No. 21
def addText(files):
try:
# Question No. 22
for file in files:
# Question No. 23
file.addLines(50 * "Test Data ")
return 1
except:
return -1

# Question No. 24
def addRandomText(files):
try:
for file in files:
randomNumber = random.randint(0,100)
# Question No. 25
file.addRandomText(randomNumber)
return 1
except:
return -1

# Question No. 26
def deleteFiles(files):
try:
for file in files:
# Question No. 27
file.deleteFile()
return 1
except:
return -1

# Question No. 28   
def closeFiles(files):
try:
for file in files:
# Question No. 29
file.closeFile()
return 1
except:
# Question No. 30
return -1
  
def main():

print('Starting program...')

# Question No. 1
files = {1:'moose.txt',2:'pizza.txt',3:'students.txt',
4:'cake.txt',5:'donuts.txt',6:'school.txt',
7:'cars.txt',8:'business.txt',9:'python.txt',10:'code.txt'}

# Question No. 2
packOfFiles = processFiles(files)

# Question No. 3
if type(packOfFiles) != list:
print('Something went wrong building the list of files')
quit()

# Question No. 4
if addText(packOfFiles) != True:
print('Something went wrong adding text to the files')
quit()

# Question No. 5   
if addRandomText(packOfFiles) != True:
print('Something went wrong adding random text to the files')
quit()

# Question No. 6
if closeFiles(packOfFiles) != True:
print('Something went wrong closing the files')
quit()

# Question No. 7
if deleteFiles(packOfFiles) != True:
print('Something went wrong deleting the files')
quit()
  
print('Program completed without error')

main()

Explanation / Answer

Question No. 1: In this line we are creating a Dictionary containing some names of the text files and assign it to the variable named 'files'

Question No. 2: Here in this line we are calling the user defind function processFiles() with the dictionary variable 'files' as argument, the process files will return a list of those file names which we are pushed as argument. and we are storing that returned list in a variable named 'packOfFiles'.

Question No. 3: Here in this bellow two lines we are checking whether the processFiles() worked properly or not the logic is if the returned value of processFiles() is not a list then it didn'n work properly and we are showing proper error message.

Question No. 4: Here we are trying to call addText() which will add some text to the file opend through 'handle' variable and if it fails we display some error message. and are quiting the program if it create something wrong.

Question No. 5: here we are calling the function addRandomText() and which obviously add some random text to the file opend through 'handle' and in failure we are displaying some error message.

Question No. 6: here we a are trying to close our file opend through handle by calling closeFiles() and ofcourse on failure we display some error message.

Question No. 7: Here we are calling the function deleteFiles() to delete the files and also display some error message on failure.

Question No. 8: This line defines a class named "File"

Question No. 9: this line defines the constructor which takes one argument('self' is a default argument of a class method) 'filename' and opens the file in 'filename'

Question No. 10: this is a defination of addLines(self,text) which adds the lines in 'text' in the file(handle)

Question No. 11: this is a defination of addRandomText(self,numberOfLines) it will add 'numberOfLines' number of line each containing "som Random Text " ( for new line)

Question No. 12: this is a defination of deleteFile(self) which will delete the file specified by name member of the class:File

Question No. 13: this is a defination of displayFileContent(self) it will display the content of the file self.name.

Question No. 14: this is a defination of closeFile(self) it closes the file specified by handle

Question No. 15: this is a defination of processFiles() it takes the dictionary and returns a list of dictionary's names

Question No. 16: this line does an exception handling which starts with a try and goes to except if something exceptional happens.

Question No. 17: creates a empty list named theFile

Question No. 18: runs a for loop based on the content of the dictionary "files"

Question No. 19: append each element of dictionary to the list "thefiles"
Question No. 20: returns theFiles

Question No. 21: this is a defination of addText(files) to add repeated lines to files

Question No. 22: for loop based on the list Files

Question No. 23: call addlines() with a string containing substring "Test Data " 50 times like "Test Data Test Data ........"

Question No. 24: this is a defination of addRandomText(files) which will add random text to files

Question No. 25: calling addRandomText(randomNumber) another varient of addRandomText() which takes a number rather than a list.

Question No. 26: this is a defination of deleteFiles(files) which delete all files in files

Question No. 27: call deleteFile() which will delete file currently assigned.

Question No. 28: this is a defination of closeFiles(files) which basically closes all the files in files

Question No. 29: call file.closeFile() to close "file"

Question No. 30: if it cant close the file it returns -1