python goto 命令的替代品?
我有一个基本上由海龟图形组成的 python 程序,它基本上是询问用户要绘制多少个正方形,然后在每个正方形之后,它使用以下方法向计数器添加 1:
counter=1
<drawing code here>
counter +=1
然后我想做一个检查方块的数量是否等于用户输入的数量,如果是,那么我想转到脚本的底部,在那里我会让它说类似完成!!< /强>。但我不知道如何让它转到脚本的某个部分,因为 python 不支持我习惯的批处理 goto 命令(我知道,goto= spaghetti code)
我发现一个简单的解决方法就是下载某人制作的一个模块,该模块允许您将 goto 命令导入到 python 中并像批量使用它一样,但我想要一个本机 python 解决方案(如果有的话)!
我当前的代码是:
from turtle import *
import time
counter=1
color("red", "blue")
down()
user=int(raw_input('how many balls do you want?'))
counter +=1
if user===counter:
# solution goes here!
else:
for step in range(24):
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
up()
goto(120,0)
down()
counter +=1
if user==counter:
#solution goes here!
else:
for step in range(24):
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
up()
goto(0,-50)
write("Done!")
time.sleep(5)
如果您有这个问题的答案或替代方案,我们将不胜感激!
I have a python program that is essentailly made up of turtle graphics, and it is basically asking the user how many squares to draw, and then after each square, it adds 1 to a counter using:
counter=1
<drawing code here>
counter +=1
And then after that I wanted to do a check to see if the number of squares is equal to the amount that the user typed in, and if it is, then I wanted to go to the bottom of the script where I would make it say something like done!!. but I dont know how to make it go to a certain part of the script as the goto command that I'm used to in batch isn't supported in python (i know, goto= spaghetti code)
I found an easy workaround is just to download a module that someone made that lets you import the goto command into python and use it just as you would in batch but I would like a native python solution if any!
my current code is:
from turtle import *
import time
counter=1
color("red", "blue")
down()
user=int(raw_input('how many balls do you want?'))
counter +=1
if user===counter:
# solution goes here!
else:
for step in range(24):
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
up()
goto(120,0)
down()
counter +=1
if user==counter:
#solution goes here!
else:
for step in range(24):
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
right(105)
forward(100)
up()
goto(0,-50)
write("Done!")
time.sleep(5)
If you have an answer or alternative to this problem it would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有帮助吗?
不要根据一长串指令来思考。相反,考虑将问题分解为更小的动作,例如“画一个球”,并将每个动作写成一个过程;然后考虑如何将这些程序结合在一起以实现您的目标。重复编写相同的代码表明您做错了。
Does this help?
Don't think in terms of a single long list of instructions. Think instead about breaking your problem apart into smaller actions, such as "drawing a ball", and write each of those actions as a procedure; then think about how to join those procedures together to accomplish your goal. Writing the same code repeatedly is a sign that you are doing it wrong.
不要检查方块的数量然后走到最后,而是循环适当的次数,每次画一个方块。
Don't check the number of squares and then go to the end, just loop the appropriate number of times instead, drawing a square each time.
一些提示:
[剪]
some hints:
[snip]
将代码分解为函数,然后在想要跳过函数的剩余代码时使用 return 。
更新:我不一定会容忍一个函数有多个退出点,正如评论者显然假设的那样;这个答案是在问题的上下文中。我对多重出口问题的看法与 蒂姆·彼得斯:
Break down your code into functions, then use
return
whenever you want to skip the remaining code of a function.UPDATE: I do not necessarily condone multiple exit points for a function, as a commenter obviously assumed; this answer is in the context of the question. My opinion on the matter of multiple-exits co-incides with that of Tim Peters: