echo.py
that reads a file and
echoes its contents line by line. For example, if file
stuff.txt
contains
This is a file. It has three lines. None if them is interesting.then at a command prompt (not the python prompt) you should see
> python echo.py stuff.txt This is a file. It has three lines. None if them is interesting. >I haven't taught this and don't expect you to figure it out. There are python keywords you just have to know.. Google is your friend. You can find out a lot from the searches
python read fileor even
python read file line by lineBe sure to acknowledge web sources that helped you - in your diary, maybe even as a comment in your program. If you find the whole program on the web you can just submit it - as long as you tell me that's what you're doing.
Hint for part of the program: read factor5.py
(available
on the course web page somewhere) to find out how to get the name of
the file to echo from the command line.
Note: I wrote this homework before I did the problems myself. When I
tested versions of my echo.py
I discovered that I was
getting an extra blank line between each two lines of the input
file. The reason is an unpleasant ambiguity in the way lines end
(carriage return or carraige return plus line feed). In a cs course
we'd have to deal with this. Here, ignore the problem. The extra lines
are OK.
average.py
that reads a file
with one floating point number per line and computes and prints their
average. Run the program this way:
> python average.py name_of_file_with_numbersWe've seen how the function
int()
converts strings to
integers. Google will help you find out how to convert strings to
floats.
Test your program to see how it behaves if some of the input lines don't represent floats, or if the the file is empty, or if the file isn't there. Report all this misbehavior, but don't try to correct it.
average_robust.py
that does what
average.py
does on good input and provides a useful
error message when it gets bad input.