1 Replies - 1030 Views - Last Post: 18 July 2009 - 12:14 AM Rate Topic: -----

#1 dumbstruckk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-July 08

Looping in Python

Posted 15 July 2009 - 08:51 PM

So I THINK I was able to convert the numbers. But then how do I make it such that the data are in the form of the colourblock?

The prof gave us these formulas:
fraction = (i+1.0)/nsteps
r = (1-fraction)*red1 + fraction*red2
To make one of those bars:
<div class="colourblock" style="background-color: rgb(100.0%,50.0%,0.0%)"> </div>
print 'style="background-color: rgb(' + str® + '%'

Here is the whole code I got so far
import cgi
form = cgi.FieldStorage()

red1 = int(form ["red1"].value)
red2 = int(form ["red2"].value)
blue1 = int(form ["blue1"].value)
blue2 = int(form ["blue2"].value)
green1 = int(form ["green1"].value)
green2 = int(form ["green2"].value)

nsteps = int(form["nsteps"].value)

def fraction():
	 fraction = i+1.0/nsteps

def colourblock(red1, red2, blue1, blue2, green1, green2):
	 b = (1-fraction)*blue1 + fraction*blue2
	 r = (1-fraction)*red1 + fraction*red2
	 g = (1-fraction)*green1 + fraction*green2

print"""Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Colour Blend</title>
<style type="text/css">
#blend {
  width: 10em;
  padding: 0;
  border: 1px solid black;
  margin-left: 3em;
}
.colourblock {
  width: 10em;
  height: 1em;
}
</style>
</head><body>
"""
print"""
<h1>Colour Blend</h1>

<p>Here is a mix of the two colours you specified:</p>


<div id="blend">
for i in range(nsteps):
	print i,

<div class="colourblock" style="background-color: rgb(r%,b%,g%)"> </div>
</div>

<hr class="footsep">
<p class="footlink">Return to <a href="http://cmpt165.cs.sfu.ca/~mja26/">the index</a>.</p>
</body>
"""




I know that the data have to be looped somehow but I have no idea where to start. Any help would be greatly appreciated.

This post has been edited by dumbstruckk: 16 July 2009 - 12:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Looping in Python

#2 deer dance   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 21-April 09

Re: Looping in Python

Posted 18 July 2009 - 12:14 AM

Try applying a for-loop to def colourblock and linking functions
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1