PYTHON
CSCI 333.01W Assignment 08
File Input Output_Exceptions_Strings
Deadline: 3/23/2020 Tue. by 11:59pm
1. (20 points, each 2 points) True or False questions:
1) (True/False) It’s a good practice to close resources/files as soon as the program no longer needs them, to prevent resource leak and make it available for other program.
Answer:
2) (True/False) The file object’s read method always returns the entire contents of a file.
Answer:
3)(True/False) String modification like s.upper() converts all alphabetic characters in s to uppercase and changes the original string s.
Answer:
4) (True/False) String comparisons are case sensitive.
Answer:
5) (True/False) File object’s readline and readlines methods both return strings in a list.
Answer:
6) (True/False) The finally clause is guaranteed to execute only if program control enters the corresponding try suite, regardless whether the function raises an exception.
Answer:
7) (True/False) If you do not specify the string alignment format, all values displayed in a field are right aligned by default.
Answer:
8) (True/False) String method find() returns -1 if it does not find the specified substring, while string method index() causes a ValueError.
Answer:
9) (True/False) with statement implicitly releases resources when its suite finishes executing.
Answer:
10) (True/False) Numbers must be converted to strings before they are written to a file.
Answer:
2. (20 points, each 2 points) Multiple choice or Fill in blank questions:
1) What method can be used to determine whether characters are numbers _______.
a) isdigit()
b) isalnum()
c) none of the above
d) both a and b
Answer:
2) The os module’s function delete a file, and function specify a new name for a file.
a) rename()
b) uname()
c) remove()
d) rmdir()
Answer:
3) Which method can change the current file position?
a) seek()
b) tell()
Answer:
4) What method can be used to make case-insensitive string comparisons
a) upper()
b) lower()
c) all of the above
d) none of the above
Answer:
5) String method _______ returns True if a string contains only letters and numbers
Answer:
6) String method returns True if a string contains only letters
Answer:
7) string.split() splits a string into a list of substrings
a) True
b) False
Answer:
8) Exception handler try/except statement must have else and finally clauses
a. True
b. False
Answer:
9) For string'\\\\Hi!\\\\' , you can represent it using a raw string ____
Answer:
10) file.readline() method returns a ____ , file.readlines() method returns a _____
a) list
b) string
c) tuple
d) dictionary
Answer:
3. (20 points) Hand-trace the following code. What is the output, or what error/problem do you observe and why?
1) (3 points)
print(f'[{"Amanda":>10}]\n[{"Amanda":^10}]\n[{"Amanda":<10}]')
|
|
Output:
|
2) (4 points)
|
name = ' Margo Magenta ' print(name.rstrip()) print(name.lstrip()) print(name.strip()) print(name)
|
|
Output:
|
3) (3 points)
|
'happy birthday'.capitalize() 'strings: a deeper look'.title() 'Python 101'.upper()
|
|
Output:
|
4) (3 points)
'1 2 3 4 5'.replace(' ', ' --> ')
|
|
Output: |
5) (4 points)
|
for word in 'to be or not to be that is the question'.split(): if word.startswith('t'): print(word, end=' ')
|
|
Output: |
4. (20 points) Write Python program
Assume a file named “numbers.txt” containing the following contents, and exists on the computer’s disk, as shown below.
# numbers.txt
22
14
-99
34
56
87
Hello
145tony
50
48
Write a program that calculates and display the average of all the numbers stored in the file by fulfilling the following requirements.
• It should include a loop or loops to read lines in the file “numbers.txt”
• It should have at least one user-defined function
• It should handle any IOError exceptions that may be raised when the file is opened, and data is read from it.
• It should handle any ValueError exceptions that may be raised when the items that are read from the file are converted to a number
(15 points) Write your program here, or copy/paste a screenshot of your Program:
(3 points) Screenshot of the output
(2 points) Save the program as “average.py”. Upload the .py file as part of your submission.
5. (20 points) Write a program that reads a file (assume “infile.txt”) containing the following text. Read each line and send it to an output file (assume “outfile.txt), preceded by line numbers. If the input file is
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
then the program produces an output file “outfile.txt”:
/* 1 */ Mary had a little lamb
/* 2 */ Whose fleece was white as snow.
/* 3 */ And everywhere that Mary went,
/* 4 */ The lamb was sure to go!
Handle exceptions if necessary.
(12 points) Write your program here, or copy/paste a screenshot of your Program:
(3 points) Screenshot of the output file content:
(2 points) Save the program as “file2file.py”. Upload the .py file as part of your submission.
6. (Try as much as you could) Random mono-alphabet cipher. The Caesar cipher, which shifts all letters by a fixed amount, is far too easy to crack. Here is a better idea. As the key, don’t use numbers but words. Suppose the key word is ”FEATHER”. Then first remove duplicate letters from the key word: yielding “FEATHR”, and append the other letters of the alphabet in reverse order:
Now encrypt the letters as follows:
Write a program that encrypts a file using this cipher. For example, the file “input.txt” contains
Hello, World!
After encryption, the file “output.txt” contains
Yhuup, Gpmut!
Note that the keyword can be other strings, the program should be able to generate different encoded alphabet based on the different keywords (allow users to input keyword). For example, if the keyword changes to “ComputerScience”, the content in the file “output.txt” would be
Runnx, Gxqnp!
(15 points) Write your program here, or copy/paste a screenshot of your Program:
(3 points) Screenshot of the outputs:
(2 points) Save the program as “encript.py”. Upload the .py file as part of your submission.
2