I have written this program and I've having a problem making a function out of it, can you help?

import math
from graphics import *

def drawface():
center = input('What is the center of the circle(enter as Point(x,y))')
size = input ('How big is the face?')
win = GraphWin()
circ = Circle(center,size)
circ.setFill('white')
eye = circ.getCenter()
y = eye.getX()
z = eye.getY()
circ.draw(win)
right = Circle(Point(int(y/1.25),int(z/1.25)),int(size/5))
right.setFill('blue')
right.setOutline('yellow')
right.draw(win)
left = right.clone()
left.move((y*.35),0)
left.draw(win)
smile1 = right.getCenter()
a = smile1.getX()
b = smile1.getY()
c = Point(a,(b*1.5))
d =c.clone()
d.move((a*.5),0)
mouth = Oval(c,d)
mouth.setOutline('red')
mouth.draw(win)


drawface()

Recommended Answers

All 3 Replies

Is the the graphics module from the Zelle book?

I'm a little confused about what you mean by 'turn it into a function' ... it looks like you already have a function. Do you mean, "shove all of the face-drawing code into a function"? If so, I would probably do this:

def drawface2(center, size):

    win = GraphWin()
    circ = Circle(center,size)
    circ.setFill('white')
    eye = circ.getCenter()
    y = eye.getX()
    z = eye.getY()
    circ.draw(win)
    right = Circle(Point(int(y/1.25),int(z/1.25)),int(size/5))
    right.setFill('blue')
    right.setOutline('yellow')
    right.draw(win)
    left = right.clone()
    left.move((y*.35),0)
    left.draw(win)
    smile1 = right.getCenter()
    a = smile1.getX()
    b = smile1.getY()
    c = Point(a,(b*1.5))
    d =c.clone()
    d.move((a*.5),0)
    mouth = Oval(c,d)
    mouth.setOutline('red')
    mouth.draw(win)

center = input('What is the center of the circle(enter as Point(x,y))')
size = input ('How big is the face?')

drawface2(center,size)

On a separate note, it was very confusing for me as the user to have to type 'Point(30,30)' to get the circle's center to be at (30,30)!

hope it helps,
Jeff

Is the the graphics module from the Zelle book?

I'm a little confused about what you mean by 'turn it into a function' ... it looks like you already have a function. Do you mean, "shove all of the face-drawing code into a function"? If so, I would probably do this:

def drawface2(center, size):
 
    win = GraphWin()
    circ = Circle(center,size)
    circ.setFill('white')
    eye = circ.getCenter()
    y = eye.getX()
    z = eye.getY()
    circ.draw(win)
    right = Circle(Point(int(y/1.25),int(z/1.25)),int(size/5))
    right.setFill('blue')
    right.setOutline('yellow')
    right.draw(win)
    left = right.clone()
    left.move((y*.35),0)
    left.draw(win)
    smile1 = right.getCenter()
    a = smile1.getX()
    b = smile1.getY()
    c = Point(a,(b*1.5))
    d =c.clone()
    d.move((a*.5),0)
    mouth = Oval(c,d)
    mouth.setOutline('red')
    mouth.draw(win)
 
center = input('What is the center of the circle(enter as Point(x,y))')
size = input ('How big is the face?')
 
drawface2(center,size)

On a separate note, it was very confusing for me as the user to have to type 'Point(30,30)' to get the circle's center to be at (30,30)!

hope it helps,
Jeff

Thank you, I'm having problems with certain aspects of the language.
can you think of a better why of Point(30,30). I would appreciate any input. liz

can you think of a better why of Point(30,30). I would appreciate any input.

Here's one option:

while True:
    try:
        x = int(raw_input("Please enter the x-coordinate of the center: "))
        y = int(raw_input("Please enter the y-coordinate of the center: "))
    except:
        print "invalid center coords.  Try again!"
    else:
         break
center = Point(x,y)

The code is larger, but it gets away from using the input statement, which is prone to abuse because it evaluates whatever the user types.

Jeff

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.