我在 Python 中不断收到“21 行错误”[关闭]

Posted

技术标签:

【中文标题】我在 Python 中不断收到“21 行错误”[关闭]【英文标题】:I keep getting '21 line errors' in Python [closed] 【发布时间】:2016-03-08 07:58:06 【问题描述】:

嗯,我遇到了这个网站,我有一个问题需要回答。我有一个起始游戏代码(它是由 Python 的 Turtle 创建的)。而且我在第 21 行不断收到语法错误。如果我更改该行的代码也没关系,它会不断弹出。希望大家帮忙。

import turtle
import time

wn = turtle.Screen()
wn.bgcolor('black')
startingTurtle = turtle.Turtle()
backHigh = turtle.Turtle()

score=0
lives=3
entrance_time=5

while entrance_time == 5:
  entrance = turtle.Turtle()
  entrance.hideturtle()
  entrance.penup()
  entrance.speed(0)
  entrance.color('#F54747')
  entrance.goto(-85, 100)
  entrance.write("Alien Shooter", font=(None, 25, "bold")
  entrance.time.sleep(0.5)
  entrance.color('#46F2AB')
  entrance.write("Alien Shooter", font=(None, 25, "bold")
  entrance.time.sleep(0.5)
  entrance.color('#F2E446')
  entrance.write("Alien Shooter", font=(None, 25, "bold")
  entrance.time.sleep(0.5)
  entrance.color('#F2466C')
  entrance.write("Alien Shooter", font=(None, 25, "bold")
  entrance_time=entrance_time-1
  entrance.time.sleep(1)

drawButton2 = turtle.Turtle()
drawButton2.hideturtle()
drawButton2.penup()
drawButton2.speed(0)
drawButton2.goto(-85, 67)
drawButton2.pendown()
drawButton2.pensize(4)
drawButton2.fillcolor('#2C6E00')
drawButton2.color('#2C6E00')
drawButton2.begin_fill()
drawButton2.left(90)
drawButton2.fd(26)
drawButton2.right(125)
drawButton2.fd(25)
drawButton2.right(113)
drawButton2.fd(24)
drawButton2.end_fill()

drawButton = turtle.Turtle()
drawButton.hideturtle()
drawButton.penup()
drawButton.speed(0)
drawButton.goto(-85, 70)
drawButton.pendown()
drawButton.pensize(4)
drawButton.fillcolor('#43A800')
drawButton.color('#43A800')
drawButton.begin_fill()
drawButton.left(90)
drawButton.fd(26)
drawButton.right(125)
drawButton.fd(25)
drawButton.right(113)
drawButton.fd(24)
drawButton.end_fill()
drawButton.penup()
drawButton.goto(-55, 74)
drawButton.color('#43A800')
drawButton.write("Press P to begin", font=(None, 13, "bold"))

highscores2 = turtle.Turtle()
highscores2.hideturtle()
highscores2.penup()
highscores2.speed(0)
highscores2.goto(93, 35)
highscores2.pendown()
highscores2.pensize(4)
highscores2.fillcolor('yellow')
highscores2.begin_fill()
highscores2.left(90)
highscores2.forward(23)
highscores2.left(90)
highscores2.forward(155)
highscores2.left(90)
highscores2.forward(23)
highscores2.left(90)
highscores2.forward(155)
highscores2.end_fill()
highscores2.penup()
highscores2.goto(-58, 41)
highscores2.color('black')
highscores2.write("Press H for highscores", font=(None, 10, "bold"))

highscores = turtle.Turtle()
highscores.hideturtle()
highscores.penup()
highscores.speed(0)
highscores.goto(93, 35)
highscores.pendown()
highscores.pensize(4)
highscores.fillcolor('yellow')
highscores.begin_fill()
highscores.left(90)
highscores.forward(23)
highscores.left(90)
highscores.forward(155)
highscores.left(90)
highscores.forward(23)
highscores.left(90)
highscores.forward(155)
highscores.end_fill()
highscores.penup()
highscores.goto(-58, 41)
highscores.color('black')
highscores.write("Press H for highscores", font=(None, 10, "bold"))

def startGame():
  backHigh.clear()
  highscores2.clear()
  drawButton2.clear()
  highscores.clear()
  drawButton.clear()
  startingTurtle.penup()
  startingTurtle.hideturtle()
  startingTurtle.color('green')
  startingTurtle.goto(100, 40)
  startingTurtle.dot(25)
  wn.onkey(None)

wn.onkey(startGame, "P")

def backHigh():
  drawButton = turtle.Turtle()
  drawButton.hideturtle()
  drawButton.penup()
  drawButton.speed(0)
  drawButton.goto(100, 70)
  drawButton.pendown()
  drawButton.pensize(4)
  drawButton.fillcolor('lightgreen')
  drawButton.begin_fill()
  drawButton.left(90)
  drawButton.forward(35)
  drawButton.left(90)
  drawButton.forward(170)
  drawButton.left(90)
  drawButton.forward(35)
  drawButton.left(90)
  drawButton.forward(170)
  drawButton.end_fill()
  drawButton.penup()
  drawButton.goto(-63, 80)
  drawButton.color('black')
  drawButton.write("Press P to begin", font=(None, 15, "bold"))

  highscores = turtle.Turtle()
  highscores.hideturtle()
  highscores.penup()
  highscores.speed(0)
  highscores.goto(93, 35)
  highscores.pendown()
  highscores.pensize(4)
  highscores.fillcolor('yellow')
  highscores.begin_fill()
  highscores.left(90)
  highscores.forward(23)
  highscores.left(90)
  highscores.forward(155)
  highscores.left(90)
  highscores.forward(23)
  highscores.left(90)
  highscores.forward(155)
  highscores.end_fill()
  highscores.penup()
  highscores.goto(-58, 41)
  highscores.color('black')
  highscores.write("Press H for highscores", font=(None, 10, "bold"))

  wn.onkey(startGame, "P")

  wn.onkey(lookHighscores, "H")

def lookHighscores():
  drawButton2.clear()
  drawButton.clear()
  highscores2.clear()
  highscores.clear()
  wn.onkey(None)
  wn.onkey(backHigh, "B")

wn.onkey(startGame, "P")
wn.onkey(lookHighscores, "H")
wn.listen()
wn.mainloop()

【问题讨论】:

哦,如果可以的话。请问一下,为什么我按字母“P”时不弹出游戏菜单,但按字母“H”时弹出高分。 你能帮我们找出第 21 行吗? 请发布完整的错误回溯。查看如何发布minimal reproducible example @Wanderer 请不要...由于很多原因,屏幕截图很糟糕。 并投票结束:印刷错误。 entry.time.sleep(0.5) 是第 21 行,问题是它之前的行缺少右括号。应该是 entry.write("Alien Shooter", font=(None, 25, "bold") ) 【参考方案1】:

尝试在 entrance.write 行的末尾添加一个结束 )。他们似乎不见了。可能还有其他错误,但这应该可以解决其中的一些错误。

至于您的P 键不起作用。 P 正在调用函数 startGame。这似乎不是正确的功能。

【讨论】:

以上是关于我在 Python 中不断收到“21 行错误”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

不断收到“在 PATH 中找不到程序 python”

为啥我在地图箭头功能中不断收到未定义的错误?

为啥我在 Swift 中的初始化程序不断收到“调用中的额外参数”

WebClient UploadFile 错误

为啥我在我的 Django 项目中不断收到此“名称‘模型’未定义”错误?

PDO 扩展配置 - 我在我的 PHP 应用程序中不断收到 PDOException“找不到驱动程序”