Re: [Tutor] can I make a while loop true again

2014-02-13 Thread Ian D
Hi 
It has taken me longer than I thought to get back to this topic. tut.
Anyway thanks. I wondered why array was being mentioned ha ha
So have I got this correct in that when I run a turtle program I am in fact 
using this forever loop, so I do not need to use a while True loop at all 
really in a turtle gui program?

 

> To: tutor@python.org
> From: __pete...@web.de
> Date: Tue, 11 Feb 2014 14:01:47 +0100
> Subject: Re: [Tutor] can I make a while loop true again
> 
> Ian D wrote:
> 
>> Thanks for the help on the last one.
>> 
>> Is it possible to restart a while loop? This doesn't work at all (surprise
>> surprise)
>> 
>> import turtle as t
>> 
>> def start():
>> global more
>> more = True
>> 
>> def stop():
>> global more
>> more = False
>> 
>> more = True
>> 
>> while True:
>> while more:
>> 
>> t.onkey(stop, "space")
>> t.onkey(start, "Up")
>> t.fd(1)
>> t.listen()
> 
> When you want your script to work like a typical GUI application you will 
> soon reach the limits with turtle. turtle tries hard to hide it, but GUIs 
> have an independent loop permanently running listening to user events and 
> small functions to respond these events. 
> 
> To have the turtle start and stop I came up with the following which looks 
> similar to normal GUI code. Instead of the explicit loop there is a function 
> `step_forward` that may or may not reschedule itself depending on the state 
> of the `running` flag.
> 
> 
> 
> import turtle
> 
> def step_forward():
> if running:
> turtle.forward(5)
> # call step_forward() again after 100 milliseconds:
> turtle.ontimer(step_forward, 100) 
> 
> def start():
> global running
> 
> if not running:
> running = True
> step_forward()
> 
> def turn_left():
> turtle.left(10)
> 
> def turn_right():
> turtle.right(10)
> 
> def stop():
> global running
> running = False
> 
> running = False
> 
> turtle.delay(0)
> turtle.onkey(start, "Up")
> turtle.onkey(turn_left, "Left")
> turtle.onkey(turn_right, "Right")
> turtle.onkey(stop, "space")
> 
> turtle.listen()
> turtle.mainloop()
> 
> As a bonus the turtle changes its direction when you hit the left or right 
> array.
> 
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>   
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner - explaining 'Flip a coin' bug

2014-02-13 Thread Neil Cerutti
On 2014-02-12, Steven D'Aprano  wrote:
> Of course attachments are "a thing" in Usenet. One of the
> reasons so few ISPs offer News services these days is because
> of the *huge* volume of attachments on binary news groups. Any
> news client that can't at least *receive* attachments is a
> major failure.

I'm living in the past, man!

-- 
Neil Cerutti

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


Re: [Tutor] Regular expressions

2014-02-13 Thread Walter Prins
Hi,

On 13 February 2014 06:44, Santosh Kumar  wrote:
> I am using ipython.
>
> 1 ) Defined a string.
>
> In [88]: print string
> foo foobar
>
> 2) compiled the string to grab the "foo" word.
>
> In [89]: reg = re.compile("foo",re.IGNORECASE)
>
> 3) Now i am trying to match .
>
> In [90]: match = reg.match(string)
>
> 4) Now i print it.
>
> In [93]: print match.group()
> foo
>
> Correct me if i am wrong, i am expecting both "foo" and "foobar", why is it
> giving
> just "foo"

A small addition to Peter's already comprehensive reply: Your regular
expression is not including what follows "foo", it is defined as
*only* the string literal "foo", so it can only ever match and return
the literal string "foo".

Try specifying "foo.*" as the regular expression.  Example session:

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 1.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help  -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

[C:/Src]|1> s='foo foobar'

[C:/Src]|2> import re

[C:/Src]|3> reg=re.compile('foo.*', re.IGNORECASE)

[C:/Src]|4> match=reg.match(s)

[C:/Src]|5> print match.group()
foo foobar



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