CS1116/CS5018 Lab 2

Before you start on this week's work, you have the opportunity to check last week's work. This is an experimental facility, which gives rough-and-ready feedback. It is not used for grading.

Fix any problems and then move on to this week's work.

Exercise A

In your public_html/cgi-bin directory, create a new directory called lab2 (no spaces, all in lowercase!!!!!!). Change its permissions! Place your work for this lab sheet into this new directory.

Copy lengths.html and lengths.py from your lab1 directory into your lab2 directory.

Modify lengths.py so that it validates the user's input. Make sure you trap as many problems as possible.

If there are no problems with the user's input, your program outputs a table, as it did previously. But, if there are problems with the user's input, the program outputs a paragraph that contains a sentence that must begin with the word 'Error!'.

Exercise B

We have been commissioned to create a Rock-Scissors-Paper game. As you probably know, in this game:

In your lab2 directory, create two files: rsp.html and rsp.py.

In rsp.html, write a form containing three radio buttons, labelled Rock, Scissors and Paper. On submit, the data is passed to the rsp.py program using the get method.

In rsp.py, write a server-side Python program that:

  1. gets the user's choice and validates it;
  2. simulates the computer's choice of Rock, Scissors or Paper by generating a random number, 0, 1 or 2, where 0 means Rock, 1 means Scissors and 2 means Paper;
  3. compares the user's input to the system's choice; and
  4. outputs the outcome using a sentence such as "Computer's Rock beats User's Scissors", or "User's Paper beats Computer's Rock", or "Computer's Rock ties with User's Rock", etc.

Exercise C

This is just a simple exercise to make you practice using (a) checkboxes and (b) dictionaries.

The idea is a fruit shop where you can buy a kilo of apples and/or bananas and/or jujubes and/or rambutans, and the program will print out the total price. Apples cost 1.59 per kilo, bananas are 1.25 per kilo and it's 27.81 and 20.84 for jujubes and rambutans, respectively.

In your lab2 directory, create two files: fruit.html and fruit.py. In fruit.html, write a form containing four checkboxes, labelled Apples, Bananas Jujubes and Rambutans. On submit, the data is passed to the fruit.py program using the get method. The program validates the user's data, and calculates and prints the total cost of the fruits that the user selected. For example, if the user selected the Apples and Jujubes checkboxes, then the program displays 29.40 (i.e. 1.59 + 27.81).

Challenge Exercises

Challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further. You can work on them together, if you wish.

Submission

There is no formal submission of this week's exercises. However, for my records, I will run a program at the weekend that checks to see whether you have done the work or not.