Home > Software engineering >  Why is my parameter not defined in Python?
Why is my parameter not defined in Python?

Time:10-29

I'm very new to python and I'm trying to call these functions in the main but whenever I run the program it says "wn" is not defined. Any reason why this won't work?

I've tried calling the function outside of the main too and still the same error.

import turtle               #1. import modules
import random

#Part A
def setupWindow(wn):
    wn = turtle.Screen()       # 2.  Create a screen
    wn.bgcolor('lightblue')

def setupTurtles():
    michelangelo = turtle.Turtle()    # 3.  Create two turtles
    leonardo = turtle.Turtle()
    michelangelo.color('orange')
    leonardo.color('blue')
    michelangelo.shape('turtle')
    leonardo.shape('turtle')
    michelangelo.up()          # 4.  Pick up the pen so we don\u2019t get lines
    leonardo.up()
    michelangelo.goto(-100,20)
    leonardo.goto(-100,-20)

    ## 5. your code goes here

    #Slowing Turtles Down
    michelangelo.speed(1)
    leonardo.speed(1)

#Race 1
def raceOne(myturtle, myturtle2):
    myturtle.forward(random.randrange(1,101))
    myturtle2.forward(random.randrange(1,101))

#Race 2
def raceTwo(myturtle, myturtle2):
    for x in range(10):
        myturtle.forward(random.randrange(0,11))
        myturtle2.forward(random.randrange(0,11))

setupTurtles()

# Part B - complete part B here
def drawShapes(myturtle):
    myturtle.down()
    for sides in [3, 4, 6, 9, 12]:
        for i in range(sides):
            leonardo.forward(50)
            leonardo.right(360/sides)
        leonardo.clear()
    return sides


def main():
    setupWindow(wn)
    setupTurtles()
    raceOne(michelangelo, leonardo)
    raceTwo(michelangel, leonardo)
    drawShapes(leonardo)

main()
wn.exitonclick()

CodePudding user response:

You can see the issue here, you pass a wn, but you also define a wn just after.

def setupWindow(wn):
    wn = turtle.Screen()       

You may define it at the beginning, and pass it after

def setupWindow(wn):
    wn.bgcolor('lightblue')


def main(wn):
    setupWindow(wn)
    setupTurtles()
    raceOne(michelangelo, leonardo)
    raceTwo(michelangel, leonardo)
    drawShapes(leonardo)


wn = turtle.Screen()  # 2.  Create a screen
main(wn)
wn.exitonclick()

CodePudding user response:

As stated by the error message, you never define wn.

You have a parameter that is called wn here:

def setupWindow(wn):
    wn = turtle.Screen()       # 2.  Create a screen
    wn.bgcolor('lightblue')

And here you passing wn as a parameter to setupWindow:

def main():
    setupWindow(wn)
    setupTurtles()
    raceOne(michelangelo, leonardo)
    raceTwo(michelangel, leonardo)
    drawShapes(leonardo)

main()

But nowhere before main() is called do you define what wn is. The first executed function is main() and it passes wn to setupWindow() but wn was never defined in this scope at this point in the execution.

You need to define wn somewhere before executing setupWindow(). The value it needs to have is up to your implementation logic but something as simple as the following could do the trick, depending on what you are trying to accomplish:

wn = None
main()

CodePudding user response:

You need to learn more about passing parameters and returning results.

I think your code should look more like this:

def setupWindow():
    wn = turtle.Screen()       # 2.  Create a screen
    wn.bgcolor('lightblue')
    return wn

# ... elided rest of code until

def main():
    wn = setupWindow()
    setupTurtles()
    raceOne(michelangelo, leonardo)
    raceTwo(michelangel, leonardo)
    drawShapes(leonardo)
    return wn

wn = main()
wn.exitonclick()

CodePudding user response:

You cannot pass the wn parameter in the setup function because the wn variable is declared after the function declaration.

CodePudding user response:

Thanks for asking the question.This issue is created to not properly referencing the function i have modified your code to make it run.Please check and further modify it.

import turtle  # 1. import modules
import random


# Part A
def setupWindow(wn):
    wn = turtle.Screen()  # 2.  Create a screen
    wn.bgcolor('lightblue')


def setupTurtles():
    michelangelo = turtle.Turtle()  # 3.  Create two turtles
    leonardo = turtle.Turtle()
    michelangelo.color('orange')
    leonardo.color('blue')
    michelangelo.shape('turtle')
    leonardo.shape('turtle')
    michelangelo.up()  # 4.  Pick up the pen so we don\u2019t get lines
    leonardo.up()
    michelangelo.goto(-100, 20)
    leonardo.goto(-100, -20)
    michelangelo.speed(1)
    leonardo.speed(1)
    raceOne(michelangelo, leonardo)
    raceTwo(michelangelo, leonardo)
    leonardo.down()
    for sides in [3, 4, 6, 9, 12]:
        for i in range(sides):
            leonardo.forward(50)
            leonardo.right(360 / sides)
        leonardo.clear()
    return sides

    ## 5. your code goes here

    # Slowing Turtles Down



# Race 1
def raceOne(myturtle, myturtle2):
    myturtle.forward(random.randrange(1, 101))
    myturtle2.forward(random.randrange(1, 101))


# Race 2
def raceTwo(myturtle, myturtle2):
    for x in range(10):
        myturtle.forward(random.randrange(0, 11))
        myturtle2.forward(random.randrange(0, 11))


setupTurtles()


# Part B - complete part B here
def drawShapes(myturtle):
    pass


def main():
    wn = ""
    setupWindow(wn)
    setupTurtles()



main()

turtle.exitonclick()
  • Related