由于某些原因,这给了我一个无效的语法错误,在所有定义的函数(在它们内部)和我真的很困惑的帮助。我不确定什么是错的,所以任何帮助对我都有帮助。非常感谢你们。
代码:
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 :(得分:2)
这一行上有一个开括号太多:
forward ((int(forward))
# 12 3 32
在开始时删除一个:
forward(int(forward))
# 1 2 21
否则Python直到 next 行才会知道缺少某些内容。
你又犯了两次同样的错误:
right ((int(right))
和
left ((int(left))
下一个问题是你试图为turtle
函数和局部变量使用相同的名称:
forward = input()
forward(int(forward))
这不起作用; Python现在在引用input()
时会看到来自forward
的字符串结果。从input()
函数a 不同的名称中提供结果:
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
答案 1 :(得分:0)
forward ((int(forward))
这是错误的一行,您必须删除这样的括号:
forward (int(forward))
答案 2 :(得分:-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()