Re: [Tutor] Turtle Issues

2018-03-06 Thread Alan Gauld via Tutor
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regex not working as desired

2018-03-06 Thread Alan Gauld via Tutor
On 06/03/18 22:17, Albert-Jan Roskam wrote:
> But the way you wrote it, the generator expression just "floats" 

Any expression can be used where a value is expected provided
that e3xpression produces a value of the required type.

A generator expression effectively produces a sequence and
the type of sequence is defined by the type of parentheses
used.

"123" -> a string
[1,2,3] -> a list
(1,2,3) -> a tuple
{1,2,3} -> a set

So when a function requires an iterable sequence you just
provide the expression(any expression) that results in an
iterable.

all(range(5))  # range 5 produces a range object which is iterable
all(n for n in [0,1,2,3,4])  # generator "equivalent" to the range

Similar things happen with tuples where the parens are actually
optional:

1,2,3   # a tuple of 3 numbers
(1,2,3) # the same tuple with parens to make it more obvious

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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Turtle Issues

2018-03-06 Thread Roger Lea Scherer
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. I've looked on StackOverflow and followed their
advice (which I can't make work) and went to the python/turtle
documentation which I tried to follow, but can't seem to make work either.

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

# ensure input is only digits
p = re.compile(r'^\d+$')
m = p.match(digits)
if m:
print(digits)
m = digits
#digits = input("Please enter an integer less than 10,000 greater
than 0:  ")
else:
print("No match")

colormode(255)
##reddish = random.randrange(255)
##greenish = random.randrange(255)
##bluish = random.randrange(255)
##pencolor(reddish, greenish, bluish)
# pencolor(random.randrange(255), random.randrange(255),
random.randrange(255))
pencolor(r, g, b)

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


# if statements for the ones position
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)

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

if digits[-1] == "4":
pu()
goto(0, 65)
seth(45)
pd()
fd(50)

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

if digits[-1] == "6":
pu()
goto(30, 100)
seth(180)
pd()
fd(35)

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

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

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

# if statements for the tens position
if digits[-2:-1] == "1":
pu()
goto(0, 100)
seth(-90)
pd()
fd(35)

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

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

if digits[-2:-1] == "4":
pu()
goto(0, 65)
seth(-45)
pd()
fd(50)

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

if digits[-2:-1] == "6":
pu()
goto(-30, 100)
seth(180)
pd()
fd(35)

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

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

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

# if statments for the hundreds position
if digits[-3:-2] == "1":
pu()
goto(0, 0)
seth(90)
pd()
fd(35)

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

if digits[-3:-2] == "3":
pu()
goto(0, 0)
seth(45)
pd()
fd(50)

if digits[-3:-2] == "4":
pu()
goto(0, 35)
seth(135)
pd()
fd(50)

if digits[-3:-2] == "5":
pu()
goto(0, 0)
seth(90)
pd()
fd(35)
lt(135)
fd(50)

if digits[-3:-2] == "6":
pu()
goto(30, 0)
seth(0)
pd()
fd(35)

if digits[-3:-2] == "7":
pu()
goto(0, 0)
seth(90)
pd()
fd(35)
lt(90)
fd(35)

if digits[-3:-2] == "8":
pu()
goto(0, 35)
seth(90)
pd()
fd(35)
rt(90)
fd(35)

if digits[-3:-2] == "9":
pu()
goto(0, 0)
seth(90)
pd()
fd(35)
lt(90)
fd(35)
lt(90)
fd(35)

# if statments for the thousands position
if digits[-4:-3] == "1":
pu()
goto(0, 0)
seth(-90)
pd()
fd(35)

if digits[-4:-3] == "2":
pu()
goto(0, 35)
seth(-90)
pd()
fd(35)

if digits[-4:-3] == "3":
pu()
goto(0, 0)
seth(-35)
pd()
fd(50)

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

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

if digits[-4:-3] == "6":
pu()
goto(-30, 0)
seth(0)
pd()
fd(35)

if digits[-4:-3] == "7":
pu()
goto(0, 0)
seth(-90)
pd()
fd(35)
rt(90)
fd(35)

if 

Re: [Tutor] Regex not working as desired

2018-03-06 Thread Steven D'Aprano
On Tue, Mar 06, 2018 at 10:17:20PM +, Albert-Jan Roskam wrote:

> > >>> all(c.isdigit() for c in '12c4')
> > False
> 
> I never understood why this is syntactically correct. It's like two 
> parentheses are missing.
> 
> This I understand:
> all((c.isdigit() for c in '12c4'))
> Or this:
> all([c.isdigit() for c in '12c4'])
> Or this:
> all((True, False))
> 
> But the way you wrote it, the generator expression just "floats" in 
> between the parentheses that are part of the all() function. Is this 
> something special about all() and any()? 

No, it is something special about generator expressions. The syntax for 
them is theoretically:

expression for x in iterable

but parentheses are required to make it unambiguous. If they are already 
inside parentheses, as in a function call:

   spam(expression for x in iterable)

the function call parens are sufficient to make it unambiguous and so 
there is no need to add an extra pair.

However, if you have two arguments, or some other compound expression, 
you need to use disambiguation parens:

spam(999, (expression for x in iterable))


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regex not working as desired

2018-03-06 Thread Albert-Jan Roskam

On Feb 27, 2018 09:50, Alan Gauld via Tutor  wrote:
>
> On 27/02/18 05:13, Cameron Simpson wrote:
>
> > hard to debug when you do. That's not to say you shouldn't use them, but 
> > many
> > people use them for far too much.
>
>
> > Finally, you could also consider not using a regexp for this particular 
> > task.
> > Python's "int" class can be called with a string, and will raise an 
> > exception
>
> And, as another alternative, you can use all() with a
> generator expression:
>
> >>> all(c.isdigit() for c in '1234')
> True
> >>> all(c.isdigit() for c in '12c4')
> False

I never understood why this is syntactically correct. It's like two parentheses 
are missing.

This I understand:
all((c.isdigit() for c in '12c4'))
Or this:
all([c.isdigit() for c in '12c4'])
Or this:
all((True, False))

But the way you wrote it, the generator expression just "floats" in between the 
parentheses that are part of the all() function. Is this something special 
about all() and any()?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor