[Tutor] University student struggling!

Andrea Semenik ams28 at sfu.ca
Tue Jul 14 01:23:47 CEST 2009


Hello Python Tutors! 

I am a non-computer science major taking a computer programming course.  That is the first red flag you should know. Second red flag, the course is poorly, I mean poorly written. third red flag, it is a distance education course, so it is done online. Needless to say I am in big trouble.  I have been working on this assignment for days and have gotten nowhere. here are the directions:

For this assignment, you will create a web script that lets the user explore RGB colours. 

On the first page the user sees, they should specify two RGB colours. When they submit this page, they should be presented with a range of colours in between the two. The intermediate colours will be calculated by your program. 

For example, if the user enters the colours red (100% red, 0% green, 0% blue) and white (100% red, 100% green, 100% blue), your program might output a page containing this: 
(image of a color box)

If you want, you can experiment with an example of a working colour explorer. You can copy HTML and CSS code from this example if you wish. 

The easiest way to create one of the blocks of colour is with a <div> like this: 

<div class="colourblock" style="background-color: rgb(100.0%,50.0%,0.0%)"> </div>
 
There are a couple of new things here. First, note that you can indicate style information with the style attribute on an HTML tag. This is generally a bad idea, but it's possible for cases just like this where you want to control the style of individual elements in very specific ways.
 
Second, you can specify an RGB colour in CSS by specifying three percentages as in rgb(100%, 50%, 0%). This isn't used as commonly as the hexadecimal method we've used in the course up to now, but it is supported by modern web browsers and will be much easier to work with in our program. In this example, the colour will have a lot of red (100%=F), half green (50%≅7) and no blue (0%=0) 

Here's how you calculate the in-between colours: I'm going to assume the percentage of red the user specified for the first colour is an integer in red1 and the amount of red for colour two is in red2. The number of steps the user requested is in nsteps. If you want the amount of red (as a percent) for colour block i, you can do this (assuming i goes from 0 to nsteps-1): 

fraction = (i+1.0)/nsteps
r = (1-fraction)*red1 + fraction*red2 

After this, the variable r will contain the correct percent (as a floating point value). The calculation for green and blue are exactly the same. (You don't have to repeat the calculation of fraction, of course.) 
Here are some other points about how the program should behave: 

•	You will have to apply a style sheet to the <div> tags so they are large enough to see. Something like this might be appropriate: 
    colourblock { width: 10em; height: 1em; } 

•	Your program must never output more than 150 total colours, no matter how many the user asks for. If the users asks for more, it should only output 150. 
Having a limitation like this is an essential security measure. If not, someone could ask for 1000000 steps; this would overload the server and someone would probably come asking you why you used so much of the University's bandwidth. 
[Actually, the course web server automatically limits the size of the page your script can create. This isn't the case on most web servers so you should get into the habit of paying attention to the amount of output you generate. Python scripts on the server are limited to 60k of output.] 

•	You don't have to worry about catching errors in the user's input. That is, don't worry about the possibility of them entering "abcd" as the number of steps. 

•	You have to be careful about the spacing when outputting the RGB percentage values. You will have to convert the numbers to strings and concatenate them. For example, this statement will output part of the style attribute: 
print 'style="background-color: rgb(' + str(r) + '%' 

•	All of your XHTML (including generated XHTML) and CSS must be valid. 

I have no idea how to begin!! can you help me?

Thank you,

Andrea Semenik


More information about the Tutor mailing list