我最近开发了一个Turtle程序来接收指令(turn,penup,penddown,circle等),并且工作正常。但是,当我尝试添加迭代一定数量的步骤的能力一定次数时,海龟程序将运行一次迭代,然后由于未知原因而无法响应。我认为这可能是因为程序迭代所需的'@',但没有任何实际工作。Python Turtle Not Responding ---没有明显的代码问题
本方案的条目的语法是:
˚F### - 用于转发 “###” 的长度
乙### - 向后为 “###” 的长度
大号### - 左转“###”程度
R ### - 右转“###”程度
C### - 用给定的(###)半径绘制圆圈
U - 拿起笔
D - 放下笔
I#... @ - 迭代指令块“#”次数
C250 I3 F050 R180 C225 @ B100 C125
CODE:
import turtle
def evaluate(commands):
"""
It's basically supposed to work
:param commands:
:return: None
"""
counter = 0
commands_length = len(commands)
while counter < commands_length:
# If the letter U is encountered, the turtle goes up
# counter increases by 2 to parse for the next character
if commands[counter] == 'U':
turtle.up()
print('up')
counter += 2
# If the letter D is encountered, the turtle goes down
# counter increases by 2 to parse for the next character
elif commands[counter] == 'D':
turtle.down()
print('down')
counter += 2
# If the letter F is encountered, the turtle moves forward
# by the amount denoted by the three following numbers
elif commands[counter] == 'F':
turtle.forward(int(commands[counter + 1: counter + 4: 1]))
print('forward(' + commands[counter + 1: counter + 4: 1] + ')')
counter += 5
# If the letter C is encountered, the turtle draws a circle
# with radius denoted by the three following numbers
elif commands[counter] == 'C':
turtle.circle(int(commands[counter + 1: counter + 4: 1]))
print('circle(' + (commands[counter + 1: counter + 4: 1]) + ')')
counter += 5
# if the letter B is encountered, the turtle moves backward
# by the amount denoted by the three following numbers
elif commands[counter] == 'B':
turtle.backward(int(commands[counter + 1: counter + 4: 1]))
print('backward(' + (commands[counter + 1: counter + 4: 1]) + '}')
counter += 5
# if the letter L is encountered, the turtle turns to its left
# by the angle denoted by the three following numbers
elif commands[counter] == 'L':
turtle.left(int(commands[counter + 1: counter + 4: 1]))
print('left(' + (commands[counter + 1: counter + 4: 1]) + ')')
counter += 5
# if the letter R is encountered, the turtle turns to its right
# by the angle denoted by the three following numbers
elif commands[counter] == 'R':
turtle.right(int(commands[counter + 1: counter + 4: 1]))
print('right(' + (commands[counter + 1: counter + 4: 1]) + ')')
counter += 5
elif commands[counter] == 'I':
counter += 3
loop = commands[counter: commands.index("@") + 1]
loop_counter = 0
loop_length = len(loop)
while loop_counter < loop_length:
for _ in range(counter + (commands.index("@") - 1)):
for x in range(loop.index('@')):
# If the letter U is encountered, the turtle goes up
# counter increases by 2 to parse for the next character
if loop[loop_counter] == 'U':
turtle.up()
print('up')
loop_counter += 2
# If the letter D is encountered, the turtle goes down
# counter increases by 2 to parse for the next character
elif loop[loop_counter] == 'D':
turtle.down()
print('down')
loop_counter += 2
# If the letter F is encountered, the turtle moves forward
# by the amount denoted by the three following numbers
elif loop[loop_counter] == 'F':
turtle.forward(int(loop[loop_counter + 1: loop_counter + 4: 1]))
print('forward(' + loop[loop_counter + 1: loop_counter + 4: 1] + ')')
loop_counter += 5
# If the letter C is encountered, the turtle draws a circle
# with radius denoted by the three following numbers
elif loop[loop_counter] == 'C':
turtle.circle(int(loop[loop_counter + 1: loop_counter + 4: 1]))
print('circle(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')')
loop_counter += 5
# if the letter B is encountered, the turtle moves backward
# by the amount denoted by the three following numbers
elif loop[loop_counter] == 'B':
turtle.backward(int(loop[loop_counter + 1: loop_counter + 4: 1]))
print('backward(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + '}')
loop_counter += 5
# if the letter L is encountered, the turtle turns to its left
# by the angle denoted by the three following numbers
elif loop[loop_counter] == 'L':
turtle.left(int(loop[loop_counter + 1: loop_counter + 4: 1]))
print('left(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')')
loop_counter += 5
# if the letter R is encountered, the turtle turns to its right
# by the angle denoted by the three following numbers
elif loop[loop_counter] == 'R':
turtle.right(int(loop[loop_counter + 1: loop_counter + 4: 1]))
print('right(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')')
loop_counter += 5
turtle.done()
def main() -> None:
user_input = input("Enter Commands:")
evaluate(user_input.upper())
turtle.mainloop()
if __name__ == '__main__':
main()
*叹息* ...有一天,我将学习如何正确地输入代码到一个Stackoverflow的问题......但今天肯定不是那一天。 –