Python Forum
User defined functions inside other user defined functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User defined functions inside other user defined functions
#1
I want to make use of a function I have defined inside another function. But I can't figure out how.

I have defined a function in for the turtle-world, jump(x, y), that creates a turtle in the turtle-world, hides the turtle and then moves it to coordinates x and y with the following code
def jump(x, y):
    import turtle

    t = turtle.Turtle()
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
I have tried the function without hiding the turtle and it works fine.

I now want to use this function to define a new function, rectangle(x, y, width, height, color), that creates a rectangle with bottom left corner in (x, y), with given width and height and fillcolor. I have written this
def rectangle(x, y, width, height, color):
    jump(x, y)
    t.setheading(270)
    t.fillcolor(color)
    t.begin_fill()
    for i in range(4):
        t.left(90)
        if i % 2 == 0:
            t.forward(width)
        else:
            t.forward(height)
    t.end_fill()
When I try to use this function Idle returns "NameError: name 'jump' is not defined". It simply won't let me use the function jump inside the function rectangle. Is it not possible to use a user defined function inside another function? or how can I solve this?

I should add that the function rectangle works perfectly when I replace the line with "jump(x, y)" with the lines in jump(x, y) as:

def rectangle(x, y, width, height, color):
    import turtle

    t = turtle.Turtle()
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.setheading(270)
    t.fillcolor(color)
    t.begin_fill()
    for i in range(4):
        t.left(90)
        if i % 2 == 0:
            t.forward(width)
        else:
            t.forward(height)
    t.end_fill()
Reply
#2
If you post all of your code in one big block would we see that jump is defined above or below rectangle?

You say defined inside another function. I do not see that, but I do see you calling it from another function.

When you have an error you should post the entire error, there may be other clues in it.

Also, you only need to import turtle once, do so at the top of your module unless there's a reason not to.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 12 589 Apr-12-2024, 11:51 AM
Last Post: deanhystad
Question Variable not defined even though it is CoderMerv 3 245 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  I'm getting a NameError: ...not defined. vonArre 2 244 Mar-24-2024, 10:25 PM
Last Post: vonArre
  run SQL without user intervention python dawid294 0 229 Jan-19-2024, 01:11 PM
Last Post: dawid294
  Passing writable arguments to functions. Assembler 11 910 Jan-15-2024, 11:32 PM
Last Post: sgrey
  partial functions before knowing the values mikisDeWitte 4 592 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  Calling functions by making part of their name with variable crouzilles 4 808 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  __name__ and __main__ in functions Mark17 3 724 Oct-12-2023, 01:55 AM
Last Post: deanhystad
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,047 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020