Linguistic Gaming with Python

Python Practice 7

In this exercise, you practice with functions and file I/O.


Exercise 1: Write a function that converts from Fahrenheit zu Celsius and returns the result (e.g., see Temperature Conversion). Embed your function in a larger program that tells the user what the program is for and asks the uer to input a certain number that can be converted. Allow for the program to loop, so that the user can input numbers over and over again until they choose to exit the program.


Exercise 2: Work through Zacharski's sentence generator in Chapter 2, section 2.5 (pp. 42-45).

Note that the exercise as described in the Chapter will only work with Python 2.X. In this case, you can load the file by using

execfile("sentence_generator.py")

and then proceed as described in the chapter. In Python 3.X, it is harder to execute files from within the interpreter (especially with functions in them). Alternatively, if you want to stick with Python 3.X, you can use this version of the program.


Exercise 3: Work through Zacharski's section 3.3.1 in Chapter 3 on string replace. Now take the text hexe.txt and

  1. replace all occurrences of "Hexe" with "Fee"
  2. write the new version into a file. Call it "fee.txt".
  3. Now write just those lines into a file called "fee-lines.txt" that originally contained the string "Hexe".

Please send the programs for all of the exercises to aikaterini-lida at uni konstanz by 11:30 am on December 12th, 2014.

End