如何设置一个解析字符串并使用turtlein Python基于字符串的解析部分执行命令的循环 [英] How do I set up a loop that parses a string and executes commands with turtlein Python bases on the parsed section of the string

查看:98
本文介绍了如何设置一个解析字符串并使用turtlein Python基于字符串的解析部分执行命令的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入将是F100S20> 60F100将向前移动乌龟100,S20将绘制半径为20的圆,并且>> 60将旋转乌龟。我无法弄清楚如何移动字符串并从每个部分获取不同的命令。



我尝试了什么:



我有一个功能设置,可以执行一个命令,如F30,但就是这样。

My input would be "F100S20>60" F100 would move the turtle forward 100, S20 would draw a circle with the radius of 20, and >60 would rotate the turtle. I cant figure out how to move along with the string and get the different commands from each part.

What I have tried:

I have a function set up that can execute one single command such as "F30" but that is it.

推荐答案

如果你知道命令的结构,那么应该很容易将它分成它的组成标记。如果每个部分是固定长度,则可以使用子字符串表示法,如果它们是可变的,则可以使用正则表达式。或者,您可以使用字符串搜索来查找特定的字段描述符。一旦你有了单独的标记,你可以使用一个简单的循环来执行每个部分,检查它的控制字符并使用它的数值作为数据。
If you know the structure of the command then it should be easy to split it into its constituent tokens. You can use substring notation if each part is fixed length or regexes if they are variable. Alternatively you can use string searching to find specific field delineators. Once you have the separate tokens you can use a simple loop to execute each part, by checking its control character and using its numeric value as data.


这篇关于如何设置一个解析字符串并使用turtlein Python基于字符串的解析部分执行命令的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆