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 13 6,750 Dec-16-2025, 05:29 PM
Last Post: zodazy
  Variable not defined, but it is defined... i think ET_ 1 443 Oct-14-2025, 08:28 PM
Last Post: snippsat
  extracting data from a user-completed fillable pdf Perry 2 1,546 Sep-25-2025, 01:49 PM
Last Post: DeaD_EyE
  Functions: why use a variable "display" in a UDF as a Boolean MMartin71 3 1,078 May-26-2025, 05:55 AM
Last Post: DeaD_EyE
  Warn user of validation error before they click submit robertkwild 1 1,321 Mar-12-2025, 03:49 AM
Last Post: Tishat73
  Locally run an APK and execute functions using Python KovyJ 0 1,176 Jan-23-2025, 05:21 PM
Last Post: KovyJ
  How to revert back to a previous line from user input Sharkenn64u 2 2,994 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Regarding Defined Functions Hudjefa 1 1,326 Nov-05-2024, 04:59 AM
Last Post: Gribouillis
  Best way to make built-in functions available to my module functions? Pedroski55 1 1,540 Oct-28-2024, 06:40 AM
Last Post: Gribouillis
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 3 9,611 Oct-01-2024, 03:32 PM
Last Post: Alex_Kirpichny

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.