I know my question is confusing, so I will clear things up here then post the code below. What I am trying to do as seen below is have a tonysetup function where it will bring a specifically styled turtle into its designated area. So I am calling the tonysetup function to the tonyspirograph function, and I am calling the torysetup function into the torystarburst function. I will give you another identical code that does this all in the main, my problem is I have to call functions into other functions.
import turtle
def tonysetup():
tony = turtle.Turtle()
tony.shape("turtle")
tony.pensize(1.05)
tony.speed(20)
def torysetup():
tory = turtle.Turtle
tory.shape("turtle")
tory.pensize(1.05)
tory.speed(20)
tory.penup()
tory.backward(75)
tory.left(90)
tory.forward(25)
tory.right(90)
tory.pendown()
def tonyspirograph():
tonysetup()
tony.speed(100)
for i in range(12):
for color in ("red", "white", "blue"):
tony.color(color)
tony.circle(62.5)
tony.circle(87.5)
tony.left(10)
tony.hideturtle()
def torystarburst():
for i in range(24):
for color in ("red", "white", "blue"):
tory.color(color)
tory.forward(150)
tory.right(145)
tory.hideturtle()
def main():
tonyspirograph()
torystarburst()
print("Star Spangled Spirograph: by *** *******")
print("Thank you veterans!")
main()
Right now when I run this code it says this "NameError: name 'tony' is not defined on line 26"
I made the next code for Veteran's Day and this is what the previous code is based on. It is all in the main as opposed to calling functions like what I need.
import turtle
def main():
tony = turtle.Turtle()
tony.shape("turtle")
tony.pensize(1.05)
tony.speed(20)
tory = turtle.Turtle()
tory.shape("turtle")
tory.pensize(1.05)
tory.speed(20)
for i in range(12):
for color in ("red", "white", "blue"):
tony.color(color)
tony.circle(62.5)
tony.circle(87.5)
tony.left(10)
tony.hideturtle()
tory.penup()
tory.backward(75)
tory.left(90)
tory.forward(25)
tory.right(90)
tory.pendown()
for i in range(24):
for color in ("red", "white", "blue"):
tory.color(color)
tory.forward(150)
tory.right(145)
tory.hideturtle()
print("Star Spangled Spirograph: by **** *******")
print("Thank you veterans!")
main()
CodePudding user response:
You've defined tony within a function, so it's only recognized in that function at the moment. You need to pass it to tonyspirograph() when you do the set up. This worked for me:
import turtle
def tonysetup():
tony = turtle.Turtle() #assign the turtle to the variable tony
tony.shape("turtle")
tony.pensize(1.05)
tony.speed(20)
return tony #return the turtle
def torysetup():
tory = turtle.Turtle() #this was missing brackets
tory.shape("turtle")
tory.pensize(1.05)
tory.speed(20)
tory.penup()
tory.backward(75)
tory.left(90)
tory.forward(25)
tory.right(90)
tory.pendown()
return tory
def tonyspirograph():
tony = tonysetup() # the setup returns the turtle tony
tony.speed(100)
for i in range(12):
for color in ("red", "white", "blue"):
tony.color(color)
tony.circle(62.5)
tony.circle(87.5)
tony.left(10)
tony.hideturtle()
def torystarburst():
tory = torysetup() # you need to do the same thing
for i in range(24):
for color in ("red", "white", "blue"):
tory.color(color)
tory.forward(150)
tory.right(145)
tory.hideturtle()
def main():
tonyspirograph()
torystarburst()
print("Star Spangled Spirograph: by *** *******")
print("Thank you veterans!")
main()
As for your frustrations, I do understand, I'm new here myself. But it is annoying when people continually ask rather than source solutions for themselves. Learn how to go through your code step by step, read the errors, refer to the documentation, Google etc. The more you can debug yourself the faster you'll learn and the more competent you'll become. Then one day when you're a gun programmer you too can be annoyed by newbies asking dumb questions.
CodePudding user response:
There are several simple bugs that keep your code from working. First is that tonysetup()
and torysetup()
create turtles, but fail to make those turtles available outside of those functions. This can be done using global variables or preferably by having the two functions return
the turtles they create to their caller, which stashes them into a variable.
The next problem is that your torystarburst()
fails to call your torysetup()
function. You get this right with tonyspirograph()
/ tonysetup()
but not with the other pair of functions.
Finally, you're passing incorrect values to the turtle speed()
method, reread the documentation. Below is my rework of your code to address the above problems and some style issues:
from turtle import Screen, Turtle
def tonysetup():
tony = Turtle()
tony.shape('turtle')
tony.pensize(1.05)
tony.speed('fastest')
return tony
def torysetup():
tory = tonysetup()
tory.penup()
tory.backward(75)
tory.left(90)
tory.forward(25)
tory.right(90)
tory.pendown()
return tory
def tonyspirograph():
tony = tonysetup()
for _ in range(12):
for color in ('red', 'white', 'blue'):
tony.color(color)
tony.circle(62.5)
tony.circle(87.5)
tony.left(10)
tony.hideturtle()
def torystarburst():
tory = torysetup()
for _ in range(24):
for color in ('red', 'white', 'blue'):
tory.color(color)
tory.forward(150)
tory.right(145)
tory.hideturtle()
def main():
tonyspirograph()
torystarburst()
print("Star Spangled Spirograph: by *** *******")
print("Thank you veterans!")
screen = Screen()
main()
screen.exitonclick()
I changed torysetup()
to call tonysetup()
as everything that tonysetup()
does is what torysetup()
would have done initially.