r/learnpython • • 10d ago

Help me troubleshoot?

Using https://pythonsandbox.com/turtle I've created this simple drawing app and it works. But when I try to save it I get a "The server returned a "405 Method Not Allowed"" error message. Tried multiple browsers, incognito mode, turned off extensions, multiple computers. No luck. Any suggestions?

import turtle

t = turtle.Turtle()

t.speed(5) # 1:slowest, 3:slow, 5:normal, 10:fast, 0:fastest

shape = "start"

while shape == "start":

shape = input("Would you like to start or stop drawing a square?")

if shape == "start":

t.forward(100)

t.right(90)

t.forward(100)

t.right(90)

t.forward(100)

t.right(90)

t.forward(100)

elif shape == "stop":

break

0 Upvotes

7 comments sorted by

View all comments

1

u/lakseol 10d ago edited 10d ago

Your code doesn't show in my browser properly. This is what I think you have:

import turtle

t = turtle.Turtle()
t.speed(5) # 1:slowest, 3:slow, 5:normal, 10:fast, 0:fastest
shape = "start"
while shape == "start":
    shape = input("Would you like to start or stop drawing a square?")
    if shape == "start":
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.forward(100)
    elif shape == "stop":
        break

That works fine, as you said, but it can be improved. You have two tests of whether you should stop the loop, the top while condition and the bottom elif shape == "stop":. You don't need two tests, you can use a simple loop (no test) and test immediately after the input() statement. This gives you simpler code:

import turtle

t = turtle.Turtle()
t.speed(5) # 1:slowest, 3:slow, 5:normal, 10:fast, 0:fastest

while True:
    shape = input("Would you like to start or stop drawing a square?")
    if shape == "stop":
        break

    t.forward(100)
    t.right(90)
    t.forward(100)
    t.right(90)
    t.forward(100)
    t.right(90)
    t.forward(100)

You could have just made the loop while True: and left the break test where it was, but I think it's more readable to keep the the loop control stuff (the start of the loop and the test to exit) all close together.