2013-12-13 45 views
-6

出於某種原因,這給了我一個無效的語法錯誤在所有定義的函數(以及它們),我真的很困惑的幫助。我不知道什麼是錯的,所以任何幫助對我都會有所幫助,非常非常感謝你。未知的錯誤與我的代碼

代碼:

from turtle import* 
x=0 
y=0 
#by importing using 'from' lets you minimise the need to write turtle.forward 

""" 
forward (50) # moves the turtle forward 50 steps 
right (90) # turns the turtle to the right 90 degress 
pencolor("red") # sets the pencolor. Needs to be written before drawing the shape 
fillcolor("blue") #always needs to be written before begin fill 
fillcolor("#FF0000") #fills with a hexadecimal color 
colormode(255) #changes the color fill option to use 0-255 rather than values from 0-1. 
fillcolor(0,255,0) #shows how to use R G B values (to fill with red in this case) 
begin_fill() #needs to be written before you begin drawing a shape 
end_fill() #needs to be written after you have finished drawing a shape 
pensize(10) # needs to be used before giving draw commands 
penup() # allows the turtle to be moved to other locations without drawing 
bgcolor("orange") # fills the background of the whole drawing canvas 
pendown() # normally the pen is down from the start but you may need to use pendown after lifting the penup 
speed(5) #from 0 to 10, 0 being the fastest. You need to specify the speed before writing the drawing instructions 
done() # completes the drawing 
""" 

# start your code here 

def turtleForward(): 
    print("How far forward do you want to go?") 
    forward = input() 
    forward ((int(forward)) 
    print("Want to go forward again? (y) (n)") 
    moreForward = input() 
    if moreForward == y: 
     turtleFoward() 



def turtleRight(): 
    print("How far right do you want to go?") 
    right = input() 
    right ((int(right)) 
    print("Want to go right again? (y) (n)") 
    moreRight = input() 
    if moreRight == y: 
     turtleRight() 


def turtleLeft(): 
    print("How far left do you want to go?") 
    left = input() 
    left ((int(left)) 
    print("Want to go left again? (y) (n)") 
    moreLeft = input() 
    if moreLeft == y: 
     turtleLeft() 

def penColour(): 
    print("What pen colour would you like?") 
    colour = input() 
    pencolor (colour) 


def menu(): 
    print("""Option 1. Set pen colour. 
Option 2. Go forward. 
Option 3. Go right. 
Option 4. Go left. 
Option 5. Quit.""") 
    optionChoice = input() 
    if optionChoice == '1': 
     pencolour() 
    elif optionChoice == '2': 
     turtleForward() 
    elif optionChoice == '3': 
     turtleRight() 
    elif optionChoice == '4': 
     turtleLeft() 
    elif optionChoice == '5': 
     x = 1 







print("Hello. Welcome to the turtle program I have made.") 
while x == y: 
    menu() 
+0

什麼意思? O.o – Flux

+0

你可以粘貼完整的回溯? – smeso

+0

它不會給回溯,只是彈出說無效的語法,併發送給我在我定義的函數中的所有我的第二個打印功能。 – Flux

回答

2

你一個開括號太多在這條線:

forward ((int(forward)) 
#  12 3  32 

在開始刪除一個:

forward(int(forward)) 
#  1 2  21 

否則Python將不知道直到下一個行有些東西丟失。

你犯同樣的錯誤另一個兩次:

right ((int(right)) 

left ((int(left)) 

接下來的問題是,你要使用相同的名稱既turtle功能和局部變量:

forward = input() 
forward(int(forward)) 

這是行不通的;當引用forward時,Python現在可以看到來自input()的字符串結果。給出的結果從input()起作用的不同名稱:

steps = input() 
forward(int(steps)) 

同樣,這也適用於其他兩個函數爲好。

print "Want to go forward again? (y) (n)" 
moreForward = input() 
if moreForward == y: 
    turtleFoward() 

您對全球y變量與在那裏,而不是'y':如果用戶想繼續前進更多一些,當你問

接下來的問題是。你可能不想在這裏使用遞歸;最好做一個循環:

def turtleForward(): 
    while True: 
     steps = input("How far forward do you want to go? ") 
     forward(int(steps)) 
     moreForward = input("Want to go forward again? (y) (n)") 
     if moreForward.lower() != 'y': 
      return 
+0

他應該從他所調用函數的名稱中選擇一個不同的變量名稱......這是一個完美的例子,爲什麼'從foo導入* '有風險。 –

+0

Martijn你是英雄。 –

+0

好的,我修復了代碼,感謝您的幫助,但它在打開烏龜抽屜時崩潰 – Flux

0
forward ((int(forward)) 

這是錯誤的線,你必須刪除這樣的支架:

forward (int(forward)) 
+0

好的,我修復了代碼,感謝您的幫助,但它打開烏龜抽屜時崩潰。 – Flux

-1

工作守則

from turtle import* 
    x=0 
    y=0 
    #by importing using 'from' lets you minimise the need to write turtle.forward 

    """ 
    forward (50) # moves the turtle forward 50 steps 
    right (90) # turns the turtle to the right 90 degress 
    pencolor("red") # sets the pencolor. Needs to be written before drawing the shape 
    fillcolor("blue") #always needs to be written before begin fill 
    fillcolor("#FF0000") #fills with a hexadecimal color 
    colormode(255) #changes the color fill option to use 0-255 rather than values from 0-1. 
    fillcolor(0,255,0) #shows how to use R G B values (to fill with red in this case) 
    begin_fill() #needs to be written before you begin drawing a shape 
    end_fill() #needs to be written after you have finished drawing a shape 
    pensize(10) # needs to be used before giving draw commands 
    penup() # allows the turtle to be moved to other locations without drawing 
    bgcolor("orange") # fills the background of the whole drawing canvas 
    pendown() # normally the pen is down from the start but you may need to use pendown after lifting the penup 
    speed(5) #from 0 to 10, 0 being the fastest. You need to specify the speed before writing the drawing instructions 
    done() # completes the drawing 
    """ 

    # start your code here 

    def turtleForward(): 
     print("How far forward do you want to go?") 
     forward = input() 
     forward (int(forward)) 
     print "Want to go forward again? (y) (n)" 
     moreForward = input() 
     if moreForward == y: 
      turtleFoward() 



    def turtleRight(): 
     print("How far right do you want to go?") 
     right = input() 
     right (int(right)) 
     print "Want to go right again? (y) (n)" 
     moreRight = input() 
     if moreRight == y: 
      turtleRight() 


    def turtleLeft(): 
     print("How far left do you want to go?") 
     left = input() 
     left (int(left)) 
     print "Want to go left again? (y) (n)" 
     moreLeft = input() 
     if moreLeft == y: 
      turtleLeft() 

    def pencolour(): 
     print("What pen colour would you like?") 
     colour = input() 
     pencolor (colour) 


    def menu(): 
     print("""Option 1. Set pen colour. 
    Option 2. Go forward. 
    Option 3. Go right. 
    Option 4. Go left. 
    Option 5. Quit.""") 
     optionChoice = str(input()) 
     if optionChoice == '1': 
      pencolour() 
     elif optionChoice == '2': 
      turtleForward() 
     elif optionChoice == '3': 
      turtleRight() 
     elif optionChoice == '4': 
      turtleLeft() 
     elif optionChoice == '5': 
      x = 1 







    print("Hello. Welcome to the turtle program I have made.") 
    while x == y: 
     menu() 
+1

不,這還不行,還有更多問題。 –

+0

使用Python 2.7.3 – MONTYHS

+1

編譯實際上並不意味着它是* working *。 **正確運行代碼**。 –