2015-04-19 61 views
0

我的python 3程序有问题。当我选择“箭头键”时,我不断收到错误消息。错误是: 该代码是如何评论,因为这是一个学校项目,需要代码真正好评论。 任何提示和或建设性的批评理解:d未定义函数名称“col”的错误。 Python 3,乌龟

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. 

+0

大家好,欢迎StackOverflow上。我投了一个票来搁置你的问题(我也下了决心),但我想让你知道为什么。这是反馈,而不是判断。请花点时间阅读帮助部分http://stackoverflow.com/help/mcve关于创建仅使用创建错误所需的代码的示例。问题文本中明确的错误描述优先于截图。如果你包含一个截图,就像timrau在他的编辑中做的那样。这有助于志愿者更方便地解决您的问题! –

+0

我对这个问题回答满意吗? –

回答

1

该错误消息是清楚的。在project(),col未定义。那么什么值可以传入control()呢?

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

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 

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

+0

感谢您的回复。我会更加注意这一点。 :d – user4803946

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.