I would like to be ablt to display 25 circles with alternating colours of red and white? But the code I produced does not work.. plz help.

from graphics import *

w = 300
h = 300
win = GraphWin("Red Circle", w, h)


center = Point(150, 150)
radius = 80
circle = Circle(center, radius)
circle.setFill('red')
circle.setWidth(2)
circle.draw(win)


win.getMouse()
win.close()

red=True
for i in range(25):
red=not red
if red:
circle.setFill('red')
else:
circle.setFill('white')

I would like to be ablt to display 25 circles with alternating colours of red and white? But the code I produced does not work.. plz help.

Code tags please:

from graphics import *

w = 300
h = 300
win = GraphWin("Red Circle", w, h)


center = Point(150, 150)
radius = 80
circle = Circle(center, radius)
circle.setFill('red')
circle.setWidth(2)
circle.draw(win)


win.getMouse()
win.close()

red=True
for i in range(25):
    red=not red
    if red:
        circle.setFill('red')
    else:
        circle.setFill('white')

This might be a simple way to do this ...

# experiment with graphics.py
# from: http://mcsp.wartburg.edu/zelle/python/graphics.py
# draw five alternating color concentric circles

from graphics import *

def main():
    win = GraphWin("My Circle", 300, 300)
    # count backwards
    for k in range(6, 1, -1):
        c = Circle(Point(150,150), 20*k)
        # alternate
        if k % 2 == 0:
            c.setFill('red')
        else:
            c.setFill('blue')
        c.draw(win)
    
    win.getMouse() # pause for click in window
    win.close()

main()

My fix keeping some more of your code:

from graphics import *
 
w = 600
h = 600
win = GraphWin("Red Circle", w, h)
 
 
center = Point(w/2, h/2)
radius = w/3
circle.setWidth(2)
  
red=True
for i in range(25):
    radius-=8
    red=not red
    if red:
        circle.setFill('red')
    else:
        circle.setFill('white')
    circle.radius = radius
    circle = Circle(center, radius)
    circle.draw(win)

 
win.getMouse()
win.close()

Is there any way I could repeat the code? on user input?, like if the user enters "2" the code is automatically repeated 2 and display 50 circles?

thx

sorry i phrased the question wrong.. how can I make 5 circles red and then next 5 white? on alternate lines? i mean 5 on one lines and 5 on the next

Divide by the width first by the count of circle and take modulo two according to vegaseat checking (but you can take out == 0 if you want). Or you can do another loop of the count of circles you want with that color inside the current loop.

Or came to my mind this other way of doing the alternation by last bit of the count, here some concentric circles as example:

from graphics import *
 
w = 600
h = 600
fill_color = ('red','white')
win = GraphWin("Red Circle", w, h)

center = Point(w/2, h/2)
radius = w/3
spacing = 8
num_each = 5
  
for i in range(25/num_each):
    for j in range(num_each):
        radius -= spacing
        if radius < 0:
            print 'Too may circles, only %i fit in.' % i
            break
        circle = Circle(center, radius)
        circle.setWidth(2)
        circle.setFill(fill_color[i&1])
        circle.draw(win)

win.getMouse()
win.close()

print in line 17 should be updated to ....% (i*num_each+j)

from graphics import *
 
w = 600
h = 600
fill_color = ('red','white')
win = GraphWin("Red Circle", w, h)

center = Point(w/2, h/2)
spacing = 8
num_each = 4
num_circles = 60

def do_circles(num_circles,num_each):  
    radius = w/3
    for i in range(num_circles/num_each):
        for j in range(num_each):
            radius -= spacing
            if radius < 0:
                print 'Too may circles, only %i fit in.' % (i*num_each+j)
                return
            circle = Circle(center, radius)
            circle.setWidth(2)
            circle.setFill(fill_color[i&1])
            circle.draw(win)

do_circles(num_circles,num_each)
win.getMouse()
win.close()

no no.. wht i actually mean is something like this.. sorry for wasting your time and effort by not being specific enough... the zero below represent circles..

00000(All Red)
00000(all White)
00000(all RED)
00000(all white)

print in line 17 should be updated to ....% (i*num_each+j)

from graphics import *
 
w = 600
h = 600
fill_color = ('red','white')
win = GraphWin("Red Circle", w, h)

center = Point(w/2, h/2)
spacing = 8
num_each = 4
num_circles = 60

def do_circles(num_circles,num_each):  
    radius = w/3
    for i in range(num_circles/num_each):
        for j in range(num_each):
            radius -= spacing
            if radius < 0:
                print 'Too may circles, only %i fit in.' % (i*num_each+j)
                return
            circle = Circle(center, radius)
            circle.setWidth(2)
            circle.setFill(fill_color[i&1])
            circle.draw(win)

do_circles(num_circles,num_each)
win.getMouse()
win.close()

thanx for the help tony but.. i was actually asking if there is a loop we could use to increase the number of circles on user input? rather than doing 50 circles.. so when user input is 2.. it increases to 50..

no no.. wht i actually mean is something like this.. sorry for wasting your time and effort by not being specific enough... the zero below represent circles..

00000(All Red)
00000(all White)
00000(all RED)
00000(all white)

Thats right so adapt my or vegaseat code. It is Daniweb policy not to give ready answer. Once you got working code we are happy to help you to optimize or if you get stuck with debuggiog.

ohh alright.. i will.. thx for the help..

I have done some of the coding..but cant seem to go any further.. could you please help to calculate length and height to assign a value to size?..based on the number of rows and columns and the size of the circles.. then all I need to do would be to alternate the colours in a loop..

win = GraphWin("Alternate Circles", Length, Height)
    for Row in range(rows):
        for Col in range(cols):
            x = size+Col*size
            y = size+Row*size
            c = Circle(Point(x,y), size/2)

thx

The size of the balls should be:

(size of the window - ((number of balls + 1) * spacing)) / number of balls
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.