未定义函数名称“col”的错误。 Python 3,乌龟

时间:2015-04-19 05:01:06

标签: python turtle-graphics

我的python 3程序有问题。当我选择'arrowkeys'时,我一直收到错误。错误是: 代码被评论为如何,因为这是一个学校项目,要求代码得到很好的评论。 任何提示和/或建设性批评都表示赞赏:D

import turtle
import time


#testing variables
x=10
y=20 
#Real variables and functions
def up2():
    bob.fd(45)
def left2():
    bob.lt(45)
def right2():
    bob.rt(45)
def stamp2():
    bob.stamp()
def setRed2():
    bob.color('red')
def back2():
    bob.bk(45)
def quitTurtles2():
    wn.bye()
def setGreen2():
    bob.color('green')

def control(col, x, y, w, s, shape):
    control('blue',-200,200,2,2,'turtle')
    wn = turtle.Screen()
    bob = turtle.Turtle()
    bob.up()
    bob.goto(x,y)
    bob.width(w)
    bob.turtlesize(s)
    bob.color(col)
    bob.shape(shape)
    bob.down()
    wn.onkey(up2, 'Up')
    wn.onkey(left2, 'Left')
    wn.onkey(right2, 'Right')
    wn.onkey(stamp2, 'space')
    wn.onkey(setRed2, 'r')
    wn.onkey(back2, 'Down')
    wn.onkey(setGreen2, 'g')
    wn.onkey(quitTurtles2, 'Escape')
    wn.listen()
    wn.mainloop()





#Set up input for window name and stuff on control

def command(): #Defining A Function Called Start
    try: #Trying The Statments Below
        global color
        global bob
        name = str(input('Please Enter A Name For Your Drawing: ')) #Variable For The Name Of The Window
        color = str(input('Please Enter A Colour For Your Turtle: ')) #Variable For The Color Of The Turtle
        def speed2():
            global speed
            speed = int(input('Please Enter A Speed For Your Turtle Between 1-15: ')) #Variable For The Speed Of The Turtle
            if speed >= 15 or speed <= 1:
                print('Please Enter A Number between 1-15 thats 2,3,4,5,6,7,8,9,10,11,12,13 or 14, Restarting...')
                print('\n')
                time.sleep(2)
                speed2()
        speed2()
        turtle.setup(500,500) #Setting Up Size Of Turtle Window
        window = turtle.Screen() #Setting Up Turtle Window
        window.title(name) #Giving Name To Turtle Window
        window.bgcolor('black') #Setting Background Color For Turtle Window
        bob = turtle.Turtle() #Giving 'Bob' Turtle Module Privileges
        bob.color(color) #Changing The Color Of Turtle
        bob.shape('turtle') #The Shape Of The Turtle
        bob.speed(speed)
    except Exception as e: #Catching Errors 
        print('Please Enter A correct Color, Restarting...') #Making My Program Unbrakable / Printing Words On The Screen
        time.sleep(3) #Making The Program Wait 3 Seconds Before Program Continues
        print ('\n') #Making A New Line 
        return command() #A goto command from batch programming implemented into python / Making program goto the start / returning the funtion start() after catching the error 

#start()# Start The Program / Start The Function Named Start()
'''
def star(): #Creating a function named star 
    spins = 0 #Setting the variable 'spins' to be set at 0
    while spins < 140: #Creating a condition where spins is less than 140
        bob.forward(spins*10) #Times variable 'spins' by 10 
        bob.right(144) #Make bob go right 144 pixels 
        bob.pensize(10) #Making the size of the pen 10
        spins += 1 #Making Spins go up each time loop play through, when spins gets to 140 function will stop
''' #Star doesn't Work atm
def square(): #automating the turtle to draw a square
    bob.forward(100)
    bob.right(90)
    bob.forward(100)
    bob.right(90)
    bob.forward(100)
    bob.right(90)
    bob.forward(100)
#speed2()
def circle(): #automating the turtle to draw a circle
    bob.circle(100,360,100)

def rectangle(): #automating the turtle to draw a rectangle
    bob.forward(200)
    bob.right(90)
    bob.forward(100)
    bob.right(90)
    bob.forward(200)
    bob.right(90)
    bob.forward(100)

def left():
    bob.left(90)

def right():
    bob.right(90)

def forward():
    bob.forward(100)
def commands():
    print('Commands are: left, right, forward, circle, square, rectangle and commands')
while True:
    command = str(input('Choose A Command: '))
    if command == ('left'):
        left()
    if command ==('right'):
        right()
    if command ==('forward'):
        forward()
    if command ==('circle'):
        circle()
    if command ==('square'):
        square()
    if command ==('rectangle'):
        rectangle()
    if command ==('stop'):
        break
        window.bye
    if command ==('Help' or command == ('commands')):
        commands()
    #if command == ('test'):
        #while True

def project():
    global choice
    choice = input(str('Would you like to control your turtle with the arrowkeys or with commands? type arrowkeys or commands: '))
    if choice == ('arrowkeys'):
       control(col, x, y, w, s, shape)
    elif choice == ('commands'):
        command()
    else:
        print('Please Type arrowkeys or commands, restarting...')
        print('\n')
        time.sleep(2)
        project()
project()
if choice == ('commands'):
    commands()

#window.mainloop() #waits for user to close window.

3 个答案:

答案 0 :(得分:1)

错误消息很明确。在project()中,未定义col。什么价值可以传递到control()呢?

此外,在control()的定义中,您可以立即致电control('blue',-200,200,2,2,'turtle')。这导致无限递归。

答案 1 :(得分:0)

您已经在那里发布了很多代码,但要按照您的方式启动,您需要understand scope

在你的功能中:

def project():
    global choice
    choice = input(str('Would you like to control your turtle with the arrowkeys or with commands? type arrowkeys or commands: '))
    if choice == ('arrowkeys'):
       control(col, x, y, w, s, shape)

您在colxywsshape

中有未定义的变量

编辑(范围的一点介绍):

a = 2
def example(b):
    a = 7
    print('Example a: ' + a)
    print('Example b: ' + b)

example(15)
print('Outside a: ' + a)

上面的代码会输出:

Example a: 7
Example b: 15
Outside a: 2

这是因为example()函数中的名称a与函数外部的a无关。它们不会覆盖或相互影响。与example()函数中的b相同。如果在函数外部有另一个名为b的变量,它不会干扰函数内的b。我希望这有帮助!

答案 2 :(得分:0)

问题是col未定义,此行中的其他参数也是如此:

control(col, x, y, w, s, shape)

尝试在拨打电话前设置这些值:

col, x, y, w, s, shape = 'blue', -200, 200, 2, 2, 'turtle'

我从作为control函数第一行的递归调用中获取了这些值,这会导致函数重复调用自身,而不会做任何有用的工作,直到Python崩溃。

以下是正确调用control的程序的编辑版本:

import turtle
import time    

#testing variables
x=10
y=20
#Real variables and functions
def up2():
    bob.fd(45)
def left2():
    bob.lt(45)
def right2():
    bob.rt(45)
def stamp2():
    bob.stamp()
def setRed2():
    bob.color('red')
def back2():
    bob.bk(45)
def quitTurtles2():
    wn.bye()
def setGreen2():
    bob.color('green')

def control(col, x, y, w, s, shape):
    wn = turtle.Screen()
    bob = turtle.Turtle()
    bob.up()
    bob.goto(x,y)
    bob.width(w)
    bob.turtlesize(s)
    bob.color(col)
    bob.shape(shape)
    bob.down()
    wn.onkey(up2, 'Up')
    wn.onkey(left2, 'Left')
    wn.onkey(right2, 'Right')
    wn.onkey(stamp2, 'space')
    wn.onkey(setRed2, 'r')
    wn.onkey(back2, 'Down')
    wn.onkey(setGreen2, 'g')
    wn.onkey(quitTurtles2, 'Escape')
    wn.listen()
    wn.mainloop()  

#Set up input for window name and stuff on control

def command(): #Defining A Function Called Start
    try: #Trying The Statements Below
        global color
        global bob
        name = str(input('Please Enter A Name For Your Drawing: ')) #Variable For The Name Of The Window
        color = str(input('Please Enter A Colour For Your Turtle: ')) #Variable For The Color Of The Turtle
        def speed2():
            global speed
            speed = int(input('Please Enter A Speed For Your Turtle Between 1-15: ')) #Variable For The Speed Of The Turtle
            if speed >= 15 or speed <= 1:
                print('Please Enter A Number between 1-15 thats 2,3,4,5,6,7,8,9,10,11,12,13 or 14, Restarting...')
                print('\n')
                time.sleep(2)
                speed2()
        speed2()
        turtle.setup(500,500) #Setting Up Size Of Turtle Window
        window = turtle.Screen() #Setting Up Turtle Window
        window.title(name) #Giving Name To Turtle Window
        window.bgcolor('black') #Setting Background Color For Turtle Window
        bob = turtle.Turtle() #Giving 'Bob' Turtle Module Privileges
        bob.color(color) #Changing The Color Of Turtle
        bob.shape('turtle') #The Shape Of The Turtle
        bob.speed(speed)
    except Exception as e: #Catching Errors
        print('Please Enter A correct Color, Restarting...') #Making My Program Unbrakable / Printing Words On The Screen
        time.sleep(3) #Making The Program Wait 3 Seconds Before Program Continues
        print ('\n') #Making A New Line
        return command() #A goto command from batch programming implemented into python / Making program goto the start / returning the funtion start() after catching the error

#start()# Start The Program / Start The Function Named Start()
'''
def star(): #Creating a function named star
    spins = 0 #Setting the variable 'spins' to be set at 0
    while spins < 140: #Creating a condition where spins is less than 140
        bob.forward(spins*10) #Times variable 'spins' by 10
        bob.right(144) #Make bob go right 144 pixels
        bob.pensize(10) #Making the size of the pen 10
        spins += 1 #Making Spins go up each time loop play through, when spins gets to 140 function will stop
''' #Star doesn't Work atm
def square(): #automating the turtle to draw a square
    bob.forward(100)
    bob.right(90)
    bob.forward(100)
    bob.right(90)
    bob.forward(100)
    bob.right(90)
    bob.forward(100)
#speed2()
def circle(): #automating the turtle to draw a circle
    bob.circle(100,360,100)

def rectangle(): #automating the turtle to draw a rectangle
    bob.forward(200)
    bob.right(90)
    bob.forward(100)
    bob.right(90)
    bob.forward(200)
    bob.right(90)
    bob.forward(100)

def left():
    bob.left(90)

def right():
    bob.right(90)

def forward():
    bob.forward(100)
def commands():
  print('Commands are: left, right, forward, circle, square, rectangle and commands')
  while True:
    command = str(input('Choose A Command: '))
    if command == ('left'):
        left()
    if command ==('right'):
        right()
    if command ==('forward'):
        forward()
    if command ==('circle'):
        circle()
    if command ==('square'):
        square()
    if command ==('rectangle'):
        rectangle()
    if command ==('stop'):
        break
        window.bye
    if command ==('Help' or command == ('commands')):
        commands()
    #if command == ('test'):
        #while True

def project():
    global choice
    choice = input(str('Would you like to control your turtle with the arrowkeys or with commands? type arrowkeys or commands: '))
    col, x, y, w, s, shape = 'blue', -200, 200, 2, 2, 'turtle'
    if choice == ('arrowkeys'):
       control(col, x, y, w, s, shape)
    elif choice == ('commands'):
        command()
    else:
        print('Please Type arrowkeys or commands, restarting...')
        print('\n')
        time.sleep(2)
        project()
project()
if choice == ('commands'):
    commands()

#window.mainloop() #waits for user to close window.