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:
- Rock beats Scissors (it blunts them);
- Scissors beats Paper (they cut it); and
- Paper beats Rock (it wraps it).
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:
- gets the user's choice and validates it;
- 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;
- compares the user's input to the system's choice; and
- 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.
- Challenge 1: Make your RSP program elegant, if you haven't already done so. Hint: modular arithmetic.
-
Challenge 2: This challenge is only worth doing if you did the previous challenge using modular arithmetic.
RSP-15 is a variation on Rock-Scissors-Paper, but with fifteen different weapons, instead of three. This image shows you the weapons and their relationships to each other (i.e. which beats which). (The original image comes from the Web site of David C. Lovelace, where you will also find that he has also worked out RPS-25 and RPS-101 — yes, with 25 and 101 weapons!)
In files called
rsp15.html
andrsp15.py
, write a program that plays the 15-weapon version of the game. -
Challenge 3: Modify
rsp15.py
so that it outputs more specific words than "beats". For example, Computer's rock blunts user's scissors. Some suggestions for these words are given towards the foot of David C. Lovelace's RSP-15 page. The challenge here is to do this elegantly.
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.