[Tutor] Turtle Issues

Alan Gauld alan.gauld at yahoo.co.uk
Tue Mar 6 19:36:56 EST 2018


On 06/03/18 18:12, Roger Lea Scherer wrote:
> I know I don't have to apologize, but I am so independent I hate asking for
> help and you guys and gals have been so helpful that now that I'm stuck
> again I'm sorry I have to. 

Thats not a problem and, in programming, is usually seen as
a strength not a weakness. Nobody likes to debug somebody
else's bad code, it's much better to get advice and write
good code first time! Independence is the fault, not
collaboration.

> All I want to do (famous last words) is randomize the color of the pen when
> the ciphers are created, but when the ciphers are created, the color
> remains black. 

You do seem to have a liking for writing an awful lot of unnecessary
code. Have you come across the concept of functions? They allow you
to take common code and encapsulate it in a single call.
Thus:

> if digits[-1] == "1":
>     pu()
>     goto(0, 100)
>     seth(90)
>     pd()
>     fd(35)
>
> if digits[-1] == "2":
>     pu()
>     goto(0, 65)
>     seth(90)
>     pd()
>     fd(35)

Becomes

def f(y):
    pu()
    goto(0, y)
    seth(90)
    pd()
    fd(35)

if digits[-1] == "1":
   f(100)
if digits[-1] == "2":
   f(65)

And ideally you use a more meaningful name than 'f'!
Maybe reposition() or similar?

The principle is known as DRY in software engineering:
- Don't Repeat Yourself

Also we already (I think it was for you) pointed
out that using an int() conversion with try/except was
easier and more reliable that a regex.
Its also much less code. So unless you really enjoy
practising your typing, it's easier to do something like:

d = input("Enter an integer less than 10,000 greater than 0:  ")
try: int(d)
except: # they are not digits so deal with it

# now use d knowing it is all digits.

> The comments below colormode are things I've tried as well.
> I include all the code in case you want to run the program.
> 
> So if you could please help like you do, that would be great and greatly
> appreciated. Thank you.
> 
> ----------------------------
> 
> from turtle import *
> import re
> import random
> 
> 
> # use turtle to draw ciphers of the Cistercian monks
> 
> digits = input("Please enter an integer less than 10,000 greater than 0:  ")
> 
> r = random.randrange(0, 255)
> g = random.randrange(0, 255)
> b = random.randrange(0, 255)

You set these values once. And never change them.

> colormode(255)

> ##reddish = random.randrange(255)
> ##greenish = random.randrange(255)
> ##bluish = random.randrange(255)
> ##pencolor(reddish, greenish, bluish)

You don't draw anything so how do you know what colour is set?
You have to draw a line to see the color.

> # pencolor(random.randrange(255), random.randrange(255),
> random.randrange(255))
> pencolor(r, g, b)

Same goes for these.

> mode("logo")   # resets turtle heading to north
> speed(0)
> ht()
> fd(100)

Now you draw something, what color is it?
(although you might need a pd() to force the pen on to the canvas)

> # if statements for the ones position
> if digits[-1] == "1":

See the comments about functions above...
And only do the extraction once:

last = d[-1]
if last == "1":
   f(...)
elif last == "2):
   f(...)
elif last = "3"
etc...

> if digits[-1] == "3":
>     pu()
>     goto(0, 100)
>     seth(135)
>     pd()
>     fd(50)

I see a new value in the last line so maybe f becomes

def f(y,d):
     pu()
     goto(0, y)
     seth(135)
     pd()
     fd(d)

It still saves a heck of a lot of typing!

> if digits[-1] == "5":
>     pu()
>     goto(0, 100)
>     seth(90)
>     pd()
>     fd(35)
>     rt(135)
>     fd(50)

And here you add two lines so either just tag the two
lines after f or define a new function:

def g(y,d,angle,d2):
   f(y,d)
   rt(angle)
   fd(d2)

I strongly suspect we can simplify this a lot more
but that should be a start.

But as to the colors, you only draw one line and you
only set the color once so it's hard to see how
randomising anything is going to be useful.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list