python goto 命令的替代品?

发布于 2024-10-07 18:28:34 字数 1184 浏览 4 评论 0原文

我有一个基本上由海龟图形组成的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无边思念无边月 2024-10-14 18:28:34

这有帮助吗?

import turtle   # don't pollute the namespace
import time

def getInt(msg):
    return int(raw_input(msg))

def drawBall():
    turtle.down()
    for i in range(96):
        turtle.right(105)
        turtle.forward(100)
    turtle.up()

def moveOver():
    turtle.goto(120,0)

def Done():
    turtle.goto(0,-50)
    turtle.write('Done!')
    time.sleep(5)

def main(): 
    turtle.color('red','blue')
    for i in range(getInt('How many balls do you want?')):
        drawBall()
        moveOver()
    Done()

if __name__=="__main__":
    main()

不要根据一长串指令来思考。相反,考虑将问题分解为更小的动作,例如“画一个球”,并将每个动作写成一个过程;然后考虑如何将这些程序结合在一起以实现您的目标。重复编写相同的代码表明您做错了。

Does this help?

import turtle   # don't pollute the namespace
import time

def getInt(msg):
    return int(raw_input(msg))

def drawBall():
    turtle.down()
    for i in range(96):
        turtle.right(105)
        turtle.forward(100)
    turtle.up()

def moveOver():
    turtle.goto(120,0)

def Done():
    turtle.goto(0,-50)
    turtle.write('Done!')
    time.sleep(5)

def main(): 
    turtle.color('red','blue')
    for i in range(getInt('How many balls do you want?')):
        drawBall()
        moveOver()
    Done()

if __name__=="__main__":
    main()

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.

被你宠の有点坏 2024-10-14 18:28:34

不要检查方块的数量然后走到最后,而是循环适当的次数,每次画一个方块。

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.

无风消散 2024-10-14 18:28:34

一些提示:

>>> def square():
    print "+--+"
    print "|  |"
    print "|  |"
    print "+--+"
    print


>>> range(5)
[0, 1, 2, 3, 4]
>>> for x in range(5):
    print x
    square()


0
+--+
|  |
|  |
+--+

1
+--+
|  |

[剪]

some hints:

>>> def square():
    print "+--+"
    print "|  |"
    print "|  |"
    print "+--+"
    print


>>> range(5)
[0, 1, 2, 3, 4]
>>> for x in range(5):
    print x
    square()


0
+--+
|  |
|  |
+--+

1
+--+
|  |

[snip]

所谓喜欢 2024-10-14 18:28:34

将代码分解为函数,然后在想要跳过函数的剩余代码时使用 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:

Well, single-exit is best because it always makes instrumentation easier and
often makes reasoning easier (up until the alternative introduces so many
goofy Boolean flags that keeping track of those is worse than the disease).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文