- When working with files, we use four new bits of Python.
Answer briefly these questions about input files.
- Write the Python statement that opens a text file named
grades.txt for input.
- Write the Python control statement that loops through the
text file one line at a time.
- Write the Python statement that closes the text file when
are done reading from it.
- Write a Python function named list_to_file()
that takes as arguments a list and a filename. The function
prints the items of the list, one per line, to a file of the
given name.
For example,
classes = ['CS 1510', 'CS 1520', 'CS 2530']
list_to_file(classes, 'schedule.txt')
will write three lines to a file named
schedule.txt.
- Consider this definition:
def f(x, y, z):
...
return total
List the steps that occur when the statement
var = f(a, 2, int(arg))
is executed.
- Write a Python function named sum_of_digits()
that takes an integer as an argument and returns the sum of
its digits. For example:
>>> sum_of_digits(1984)
22
Recall that we can separate a number into its last digit
and the rest of its digits using % and
//, respectively.
- What does this program print?
def mystery(x, z, y):
x = x + y
y = y + z
return y * z
x = 1
y = 3
z = 2
x = mystery(z, x, y)
print(x, y, z)
- Write a Python function named unique() that
takes a list as an argument and returns a list that contains
no duplicates. For example:
>>> unique([1, 2, 1, 2, 3, 1, 4, 2, 3, 4])
[1, 2, 3, 4]
- Consider this code:
def empty_grid(rows, cols):
grid = []
row = []
for i in range(cols):
row.append(0)
for i in range(rows):
grid.append(row)
return grid
This function does create a grid of independent cells, as
expected.
- What is wrong with this code?
- Why does it work this way?
- Suggest a way to fix the code.
- Write a Python function named get_selection()
that takes two numbers as arguments low and
high. The function asks the user to enter a
number between low and high,
inclusive, and returns it. If the user enters an invalid
choice, the function keeps asking. For example:
>>> choice = get_selection(1, 4)
Enter value between 1 and 4: 6
Sorry, 6 is not in range.
Enter value between 1 and 4: 0
Sorry, 0 is not in range.
Enter value between 1 and 4: 3
>>> print(choice)
3
- Answer briefly each of these questions about functional
decomposition:
- What is the relationship between a module at Level
n and the module it is connected to on
n-1?
- What is the difference between a concrete step
and an abstract step?
- Why is functional cohesion as valuable
property for a module to have?
- Unix provides a handy little program named wc
that prints the number of lines, words, and characters in a
text file.
> wc card-exercise.txt
13 448 2572 card-exercise.txt
Write a Python function named counts that
takes a file as an argument (not a filename) and returns
a tuple containing the number of lines, words, and characters
in the file.
With your function, we can implement wc
with this code:
lines, words, characters = counts( text_file )
print('{:8d}{:8d}{:8d} {:s}'.format(lines, words, characters, filename))