I want to be able to draw an eye for a colour inputted by the user. If the user enters the colour green,brown or blue then the eye should be drwan orelse an error message saying "not a valid colour" should be outputted

from graphics import *
def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)

def eyePicker():
    colour=raw_input("Please enter the colour: ")
    


def drawEye(win, centre, radius, colour):
    w = 250
    h = 250
    win = GraphWin("Eye", w, h)
    # center should be at 1/2 width and height
    centre = Point(w//2, h//2)
    drawCircle(win, centre, 40, "colour")
    drawCircle(win, centre, 20, "colour")
    drawCircle(win, centre, 10, "colour")
    win.getMouse() 
    win.close()
drawEye()

I modified your code to give you a first running version

from graphics import *
def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)

def eyePicker():
    colour = raw_input("Please enter the colour: ")
    return colour # <- return the picked colour
    


def drawEye( #win, centre, radius, colour # <- don't need arguments since they are computed in the function's body.
     ):
    w = 250
    h = 250
    win = GraphWin("Eye", w, h)
    # center should be at 1/2 width and height
    centre = Point(w//2, h//2)
    colour = eyePicker() # <- get the colour
    drawCircle(win, centre, 40, colour)
    drawCircle(win, centre, 20, colour)
    drawCircle(win, centre, 10, colour)
    win.getMouse() 
    win.close()
drawEye()

how would you output an error if they enter a colour which is not green,brown or blue?

You can add a loop in eyePicker until the user enters a valid colour:

from graphics import *

eye_colours = set("""green brown blue""".strip().split())

def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)

def eyePicker():
    while True:
      colour = raw_input("Please enter the colour: ").strip()
      colour = colour.lower()
      if colour in eye_colours:
        return colour
      else:
        print("Invalid colour '{0}'.".format(colour))
        print("Possible values are {0}".format(tuple(eye_colours)))
    


def drawEye():
    w = 250
    h = 250
    win = GraphWin("Eye", w, h)
    try:
      # center should be at 1/2 width and height
      centre = Point(w//2, h//2)
      colour = eyePicker()
      drawCircle(win, centre, 40, colour)
      drawCircle(win, centre, 20, colour)
      drawCircle(win, centre, 10, colour)
      win.getMouse()
    finally:
      win.close()

drawEye()

Edited 7 Years Ago by Gribouillis: n/a

I'd like to know how you could ensure the user can only select Green, Blue or Red using if/else functions, if possible?

You can write

if colour in ("Green", "Blue", "Red"):
    ... # do something
else:
    ... # do something else

You can write

if colour in ("Green", "Blue", "Red"):
    ... # do something
else:
    ... # do something else

Great, thanks.

This question has already been answered. Start a new discussion instead.