从 PyCharm 导入到 Repl.it 后 Python 代码不起作用(ParseError: bad input)
Posted
技术标签:
【中文标题】从 PyCharm 导入到 Repl.it 后 Python 代码不起作用(ParseError: bad input)【英文标题】:Python code doesn't work after imported from PyCharm to Repl.it (ParseError: bad input) 【发布时间】:2022-01-13 19:19:12 【问题描述】:我正在 PyCharm 中开发一个简单的单点透视 3D 引擎,它使用 Turtle 来渲染图像。代码只是渲染了三颗(制作不良的)星星漂浮并改变颜色。我也曾在 repl.it 网站上工作过好几个小时,并认为我会在那里“上传”我的代码,以便我可以在有时间的时候继续从其他地方在线处理它。但似乎 PyCharm 和 Repl.it 中的两个编译器之间存在差异:在 PyCharm 中运行良好的完全相同的代码只是在 Repl.it 中出现编译器错误。确切的错误是ParseError: bad input on line 59
,这真的很烦人。下面是代码。我不希望任何人阅读所有代码,而只是快速查看该特定行,看看是否只是我是个白痴。 只是提醒一下,它很长!
import turtle
import time
import math
turtle.getscreen()
turtle.colormode(255)
turtle.bgcolor(0, 0, 0)
turtle.pensize(7)
turtle.speed(99)
def draw_line(start_x, start_y, start_z, end_x, end_y, end_z, shift_x, shift_y, shift_z):
fov = 10
turtle.penup()
print("Drawing Line -- First pos: " + str(fov*((start_x+shift_x)/(start_z+shift_z))) + ", Second pos: " + str(fov*((start_y+shift_y)/(start_z+shift_z))))
turtle.goto(fov*((start_x+shift_x)/(start_z+shift_z)), fov*((start_y+shift_y)/(start_z+shift_z)))
turtle.pendown()
turtle.goto(fov*((end_x+shift_x)/(end_z+shift_z)), fov*((end_y+shift_y)/(end_z+shift_z)))
turtle.penup()
def draw_star(pos_x, pos_y, pos_z):
turtle.tracer(0, 0)
draw_line(-10, -10, 1, 0, 10, 1, pos_x, pos_y, pos_z)
draw_line(0, 10, 1, 10, -10, 1, pos_x, pos_y, pos_z)
draw_line(10, -10, 1, -10, 3, 1, pos_x, pos_y, pos_z)
draw_line(-10, 3, 1, 10, 3, 1, pos_x, pos_y, pos_z)
draw_line(10, 3, 1, -10, -10, 1, pos_x, pos_y, pos_z)
draw_line(-10, -10, 1.1, 0, 10, 1.1, pos_x, pos_y, pos_z)
draw_line(0, 10, 1.1, 10, -10, 1.1, pos_x, pos_y, pos_z)
draw_line(10, -10, 1.1, -10, 3, 1.1, pos_x, pos_y, pos_z)
draw_line(-10, 3, 1.1, 10, 3, 1.1, pos_x, pos_y, pos_z)
draw_line(10, 3, 1.1, -10, -10, 1.1, pos_x, pos_y, pos_z)
draw_line(-10, -10, 1, -10, -10, 1.1, pos_x, pos_y, pos_z)
draw_line(0, 10, 1, 0, 10, 1.1, pos_x, pos_y, pos_z)
draw_line(10, -10, 1, 10, -10, 1.1, pos_x, pos_y, pos_z)
draw_line(-10, 3, 1, -10, 3, 1.1, pos_x, pos_y, pos_z)
draw_line(10, 3, 1, 10, 3, 1.1, pos_x, pos_y, pos_z)
print("Finished drawing star at " + str(pos_x) + " " + str(pos_y) + " " + str(pos_z))
turtle.update()
i = 0
i2 = 0
i3 = 0
clr = 0
back = False
while True:
turtle.clear()
valuelist = [math.cos(i/1.2), math.cos(i2/1.2), math.cos(i3/1.2)]
sortedlist = sorted(valuelist)
match (sortedlist.index(valuelist[len(valuelist) - 3])):
case 0:
turtle.pencolor(clr, 255-clr, clr)
turtle.pensize(5-(math.cos(i/1.2)*3))
draw_star(math.cos(i)*10-30, math.sin(i)*10, math.cos(i/1.2)/3)
case 1:
turtle.pencolor(255-clr, clr, clr)
turtle.pensize(5 - (math.cos(i2 / 1.2) * 3))
draw_star(math.cos(i2)*10, math.sin(i2)*10, math.cos(i2/1.2)/3)
case 2:
turtle.pencolor(clr, 255-clr, clr)
turtle.pensize(5 - (math.cos(i3 / 1.2) * 3))
draw_star(math.cos(i3)*10+30, math.sin(i3)*10, math.cos(i3/1.2)/3)
match (sortedlist.index(valuelist[len(valuelist) - 2])):
case 0:
turtle.pencolor(clr, 255 - clr, clr)
turtle.pensize(5 - (math.cos(i / 1.2) * 3))
draw_star(math.cos(i) * 10 - 30, math.sin(i) * 10, math.cos(i / 1.2) / 3)
case 1:
turtle.pencolor(255 - clr, clr, clr)
turtle.pensize(5 - (math.cos(i2 / 1.2) * 3))
draw_star(math.cos(i2) * 10, math.sin(i2) * 10, math.cos(i2 / 1.2) / 3)
case 2:
turtle.pencolor(clr, 255 - clr, clr)
turtle.pensize(5 - (math.cos(i3 / 1.2) * 3))
draw_star(math.cos(i3) * 10 + 30, math.sin(i3) * 10, math.cos(i3 / 1.2) / 3)
match (sortedlist.index(valuelist[len(valuelist) - 1])):
case 0:
turtle.pencolor(clr, 255 - clr, clr)
turtle.pensize(5 - (math.cos(i / 1.2) * 3))
draw_star(math.cos(i) * 10 - 30, math.sin(i) * 10, math.cos(i / 1.2) / 3)
case 1:
turtle.pencolor(255 - clr, clr, clr)
turtle.pensize(5 - (math.cos(i2 / 1.2) * 3))
draw_star(math.cos(i2) * 10, math.sin(i2) * 10, math.cos(i2 / 1.2) / 3)
case 2:
turtle.pencolor(clr, 255 - clr, clr)
turtle.pensize(5 - (math.cos(i3 / 1.2) * 3))
draw_star(math.cos(i3) * 10 + 30, math.sin(i3) * 10, math.cos(i3 / 1.2) / 3)
i = i + 0.2
i2 = i2 - 0.25
i3 = i3 + 0.3
if clr > 245:
back = True
if clr < 15:
back = False
if back:
clr = clr - 15
else:
clr = clr + 15
time.sleep(0.1)
【问题讨论】:
【参考方案1】:Python 的 match
语法是 Python 3.10 的新语法。如果您提交给的解释器是早期版本,您将在第 59 行收到 SyntaxError。
请注意,3.10 版是 2 个月前刚刚发布的。这仍然是最前沿的语法。
【讨论】:
谢谢!我明天一定会检查出来的。您认为您可以添加使用match
的替代方法吗?以上是关于从 PyCharm 导入到 Repl.it 后 Python 代码不起作用(ParseError: bad input)的主要内容,如果未能解决你的问题,请参考以下文章
从用户中删除角色,然后将它们添加回 Discord.py(使用 Repl.it)
SyntaxError:意外的令牌'?'在 repl.it 中,因为我更新到 discord.js V13
为啥从命令行运行时导入失败,而从 PyCharm 运行导入成功?