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


Messages In This Thread
User defined functions inside other user defined functions - by WildP - Jan-29-2020, 12:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  two functions working in a strange way zapad 1 96 9 hours ago
Last Post: deanhystad
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 984 Apr-24-2024, 05:47 AM
Last Post: Bronjer
Question Variable not defined even though it is CoderMerv 3 334 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  I'm getting a NameError: ...not defined. vonArre 2 332 Mar-24-2024, 10:25 PM
Last Post: vonArre
  run SQL without user intervention python dawid294 0 243 Jan-19-2024, 01:11 PM
Last Post: dawid294
  Passing writable arguments to functions. Assembler 11 1,042 Jan-15-2024, 11:32 PM
Last Post: sgrey
  partial functions before knowing the values mikisDeWitte 4 642 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 616 Nov-23-2023, 02:53 PM
Last Post: rob101
  Calling functions by making part of their name with variable crouzilles 4 861 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  __name__ and __main__ in functions Mark17 3 764 Oct-12-2023, 01:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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