【发布时间】:2020-01-07 12:30:05
【问题描述】:
我只是在输入书中的代码,但是这个代码不起作用
那本书说这很正常,但我没有工作
这是我的代码
import turtle as t
import random
#t.bgcolor("black")
def turn_up():
t.left(2)
def turn_down():
t.right(2)
def fire():
ang = t.heading
while t.ycor > 0:
t.fd(15)
t.rt(5)
d = t.distance(target,0)
t.sety(random.randint(0,100)
(sety)
if d < 25 :
t.color("green")
t.write("Awsome!",False,"center",("",10))
else:
t.color("red")
t.write("Miss",False,"center",("",10))
t.color("black")
t.goto(-200,10)
t.setheading(ang)
t.up()
t.goto(-300,0)
t.down()
t.goto(300,0)
target = random.randint(50,150)
t.pensize(3)
t.color("green")
t.up()
t.goto(target - 25,2)
t.down()
t.goto(target + 25,2)
t.color("black")
t.pensize(2)
t.up()
t.goto(-200,10)
t.setheading(20)
t.onkeypress(turn_up,"Up")
t.onkeypress(turn_down,"Down")
t.onkeypress(fire,"space")
t.listen
我收到此错误:
File "main.py", line 19
if d < 25 :
^
SyntaxError: invalid syntax
我只是想要here 的海龟炮游戏,但没有成功!
你能告诉我为什么会发生这个错误吗?
【问题讨论】:
-
在链接代码中,
if在函数内部。你的不是。您在此处发布的代码会比if更早中断,因为未定义target。 -
@h4z3 不,首先出现语法错误。
-
@StefanPochmann 好吧,OP 没有说明确切的问题是什么,所以我不想假设它是语法错误。但你是对的。我怀疑这可能是
if内部的轻微过度缩进。 -
@h4z3 不,
:需要类似于else,以便继续前面三行开始的持续表达式。 -
d不是整数类型。不是吗?