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
Question Variable not defined even though it is CoderMerv 3 113 Yesterday, 02:13 PM
Last Post: Larz60+
  I'm getting a NameError: ...not defined. vonArre 2 173 Mar-24-2024, 10:25 PM
Last Post: vonArre
  run SQL without user intervention python dawid294 0 205 Jan-19-2024, 01:11 PM
Last Post: dawid294
  Passing writable arguments to functions. Assembler 11 824 Jan-15-2024, 11:32 PM
Last Post: sgrey
  partial functions before knowing the values mikisDeWitte 4 536 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  Calling functions by making part of their name with variable crouzilles 4 748 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  __name__ and __main__ in functions Mark17 3 679 Oct-12-2023, 01:55 AM
Last Post: deanhystad
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 991 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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