Python Exercises lab 5

5.1 Prerequisites and learning outcomes

Before you start you are expected to have completed lab 4.

At the end of this lab you will have:

5.2 Creating dictionaries

a. Using the Python command line, create a dictionary called cat with suitable name/value pairs which give a cat 9 lives, a name of "Purcell", an appetite of "greedy" and an age of 5 cat-years.

Confirm that you can print this dictionary out correctly. Write down how it gets printed out in your logbook.

b. Write a Python program which prompts the user for name/value pairs until the user enters an asterisk (*) to quit, assigning each name value pair into the dictionary. Get a list of dictionary keys and print out their names and values in 2 columns. E.G the dictionary you created above (but this time using your program) might be printed as follows:

Name            Value
====            =====
appetite        greedy
cat-years       5
lives           9
name            Purcell

Hint: you can use the program in the Course Notes section 5.3 as a starting point, but note that you are creating a single dictionary with arbitrary keys requested by the user, not a list of dictionaries with hard-coded keys.

c. Use the sort() method on the list of keys so that the dictionary is printed out in sorted name order, as in the above example.

5.3 A list of dictionaries

a. Extend the program in the Course Notes 5.3 with the additional functions for loading and saving in Notes 5.5. Integrate these functions into the main program. Test it, debug it and get it fully working.

b. Write a function delete() which can be used to delete dictionary records from this database. This will have to prompt the user for the name to delete, and then search through the list for the record with this name. To do this you may wrap the following code into a suitable function and debug it:

student=raw_input("enter name to delete")
for rec in db:
  if rec["name"]==student:
     del db[i]

Then code a menu function which operates within a while loop and can either call the add function, or the delete function or quits the loop and saves the database.

5.4 Finding a value in a list of dictionaries

a. If you had more than 1 student with the same name which would get deleted ? How would you make sure that the students marks database program you have helped develop in exercise 5.3 doesn't allow more than 1 student to be added with the same name ? What additional data might be included within the database in case there were duplicate student names within a course ? Write your answers into your logbook.

b. To get a similar search that you had in your delete function to work in your add function, so that you don't add a student that already exists, put the code that does this search into a find() function, which takes the name of the student to find as a parameter and returns the index of the list at which the student is found or -1 if not found. Call this find() function both from your add() and delete() functions.

c. Print out the working source code and paste it into your logbook.

5.5 An HTML colouring program

a. Write a Python program which prompts the user for the name of an HTML file and prompts the user to input a background colour. You might allow options including red, green and blue etc. and allow any 6 digit hexadecimal colour value e.g: #8F3A00 . Use one or more regular expressions (e.g. in if - elif tests) to check for valid input. Invalid user input must be rejected.

b. Your program must input this file and determine if there already is a bgcolor attribute within the <BODY> tag. If there is, remove it and substitute the new colour value. Otherwise insert the colour as a new attribute. You should use regular expressions to implement the relevant searches and substitutions. Write the modified HTML source back to the original file. You can reuse the source code within the course notes if you wish to meet this requirement.