Homework for Week 8
Files
Watch lecture videos:
Slides in English
Textbook in English
Quiz
Go to Moodle and solve the quiz on files.
Examples
NB! Make sure that the text files (.txt) are in the same folder as your Python code. Otherwise Python will not find the files.
Example 1. Shoe size
The file footlengths.txt contains data about foot lengths. The following program reads the data from the file, calculates the suitable shoe size for each foot length and prints the result. If the foot length cannot be converted into a number for some reason, then the program prints Invalid input and proceeds to the next line in the file.
def shoe_size(length): return round(1.5 * (length + 1.5)) ffile = open("footlengths.txt") for foot in ffile: try: f = float(foot) s = shoe_size(f) print("Foot length:", f, "Suitable shoe size is", s) except: print("Invalid input") ffile.close()
Example 2. Uppercase
The following program prompts the user for filename, reads the file and prints the content of the file (line by line) in uppercase. The program finishes its work nicely regardless of whether the file exists in the folder or not.
fname = input("Please enter the file name: ") try: ffile = open(fname) except: print("File cannot be opened:", fname) else: for line in ffile: print(line.strip().upper()) ffile.close()
Exercises
1. Numbered lines
Write a program that prompts the user for filename and outputs all lines from the file, numbering them with consecutive integers. The program should finish nicely regardless of whether the file exists in the folder or not (use try-except).
For example, if we have the file names.txt with the following content:
Jon Snow Arya Stark Tyrion Lannister Eddard Stark Melisandre
then the program should output:
Enter filename: names.txt 1. Jon Snow 2. Arya Stark 3. Tyrion Lannister 4. Eddard Stark 5. Melisandre
2. Taxi prices
The party ended and everybody starts to go home. The file taxiprices.txt contains information about taxi prices, where each line consists of taxi name, base fee, and kilometer price, separated by commas. The program should ask for distance to home in kilometers and output the name of the cheapest taxi, according to the prices in taxi price file.
Example
Content of the file taxiprices.txt:
Maxitaxi,2.0,0.6 Friendshare,10,0 Waldo takso,1.0,1.0
Output of the program:
Enter distance in kilometers: 7 Maxitaxi is the cheapest.
Taxi fare is computed by the formula base fee + kilometer price × distance in kilometers.
The program should not ask for filename. It can be assumed that the file taxiprices.txt exists.
Hint
>>> s = 'xx,yy,zz'.split(',') >>> s ['xx', 'yy', 'zz'] >>> s[1] 'yy'
3. Machine translation
Write a program that prompts for two filenames. The first name belongs to an existing text file, the second name is the name of a new file that may not yet exist.
The task of the program is to take the content of the first file, replace every word "Hello" with "Hi" and write the result in the second file. On the screen it should print how many replacements were made.
Example
Content of the file source.txt:
Hello! Hello Kitty Hello-Hello
Input and output:
Enter source file name: source.txt Enter destination file name: destination.txt 4 replacements were made.
Content of the resulting file destination.txt:
Hi! Hi Kitty Hi-Hi
Hint
>>> 'abcabca'.count('ab') 2
Submit your solutions
Submit your solutions of exercises 1, 2, and 3 in Moodle under the names home1.py, home2.py, and home3.py.