2015-04-19 88 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.