Even if we assume that the user always types in valid input (floats), there is s
ID: 3648844 • Letter: E
Question
Even if we assume that the user always types in valid input (floats), there is still a test case that could make our program to crash. For 5 extra points, identify this test case and add code that outputs the correct answer for this test case and prevents the program from crashing.-can anyone help me to understand what this is mean?
Explanation / Answer
While a program is running, its data is in memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a hard drive, floppy drive, or CD-ROM. When there are a large number of files, they are often organized into directories (also called "folders"). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you're done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a file, you specify its name and indicate whether you want to read or write. Opening a file creates a file object. In this example, the variable f refers to the new file object. >>> f = open("test.dat","w") >>> print f The open function takes two arguments. The first is the name of the file, and the second is the mode. Mode "w" means that we are opening the file for writing. If there is no file named test.dat, it will be created. If there already is one, it will be replaced by the file we are writing. When we print the file object, we see the name of the file, the mode, and the location of the object. To put data in the file we invoke the write method on the file object: >>> f.write("Now is the time") >>> f.write("to close the file") Closing the file tells the system that we are done writing and makes the file available for reading: >>> f.close() Now we can open the file again, this time for reading, and read the contents into a string. This time, the mode argument is "r" for reading: >>> f = open("test.dat","r") If we try to open a file that doesn't exist, we get an error: >>> f = open("test.cat","r") IOError: [Errno 2] No such file or directory: 'test.cat' Not surprisingly, the read method reads data from the file. With no arguments, it reads the entire contents of the file: >>> text = f.read() >>> print text Now is the timeto close the file There is no space between "time" and "to" because we did not write a space between the strings. read can also take an argument that indicates how many characters to read: >>> f = open("test.dat","r") >>> print f.read(5) Now i If not enough characters are left in the file, read returns the remaining characters. When we get to the end of the file, read returns the empty string: >>> print f.read(1000006) s the timeto close the file >>> print f.read() >>> The following function copies a file, reading and writing up to fifty characters at a time. The first argument is the name of the original file; the second is the name of the new file: def copyFile(oldFile, newFile): f1 = open(oldFile, "r") f2 = open(newFile, "w") while True: text = f1.read(50) if text == "": break f2.write(text) f1.close() f2.close() return The break statement is new. Executing it breaks out of the loop; the flow of execution moves to the first statement after the loop. In this example, the while loop is infinite because the value True is always true. The only way to get out of the loop is to execute break, which happens when text is the empty string, which happens when we get to the end of the file.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.