开发者

python alternative to the goto command?

开发者 https://www.devze.com 2023-01-31 03:05 出处:网络
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:

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 turtl开发者_JAVA技巧e 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!


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.


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:

>>> 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]


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消