Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Marc Poulin

--- Dick Moores [EMAIL PROTECTED] wrote:
 
 Andrei's
 Write your own iterator:
   def hugerange(minval, maxval):
 ... val = minval
 ... while val  maxval:
 ... yield val
 ... val += 1
 
 All 3 are essentially the same, aren't they. Which
 makes me feel even 
 dumber, because I don't understand any of them. I've
 consulted 3 
 books, and still don't understand the use of yield.
 

This is an example of a coroutine.
See http://en.wikipedia.org/wiki/Coroutine

def hugerange(minval, maxval):
val = minval
while val  maxval:
yield val
### return value of val to calling routine
### at this point and place
### hugerange() function temporarily
### on hold ...
###
### next call to hugerange()
### resumes at this point with
### all previous variable values
### still intact ...
val += 1


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help

2006-10-01 Thread Marc Poulin
--- Kefka Palazzo [EMAIL PROTECTED] wrote:

 I am trying to learn a programming language good for
 programming entire
 games (core functions too) similar to both the Final
 Fantasy and Metroid
 series. From the book I'm learning from (Python
 Programming for the Absolute
 Beginner, by Michael Dawsom) it seems like within a
 week or two I'll have a
 basic grasp on most of the functions, but I want to
 know if it will be worth
 it for what I need it for or if I would be better
 off learning something
 different (and harder .)

From the http://panda3d.org website:

Panda3D is a 3D engine: a library of subroutines for
3D rendering and game development. The library is C++
with a set of Python bindings. Game development with
Panda3D usually consists of writing a Python program
that controls the the Panda3D library.

Panda3D was developed by Disney for their massively
multiplayer online game, Toontown. It was released as
free software in 2002. Panda3D is now developed
jointly by Disney and Carnegie Mellon University's
Entertainment Technology Center


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for an edutainment-type introduction to programming book

2006-09-29 Thread Marc Poulin
Look here:
www.ceebot.com

Not a book, but it might be what you are looking for.


--- Abel Daniel [EMAIL PROTECTED] wrote:

 
 Hi!
 
 I'm looking for a book to give to my younger brother
 as a birthday
 present. He is 13 years old, had some experience
 with logo (but not
 much, so he knows about simple instructions and
 loops, but not about,
 say, algorithms), and is fairly comfortable around
 computers. He
 sometimes mentions that he would like to learn
 programming, but so far
 my only attempt to teach him was an absolute
 failure, due mostly to my
 total lack of pedagogical skills. (I tried to start
 with the concept
 of abstract objects, with predictable effects...)
 
 What I am looking for is a book thats:
 
 1) simple, and fun enough so that he can learn from
 it without my
 continous assistence. (Of course, I can answer
 questions, but the idea
 is that I don't want to walk him through all of it.)
 
 2) doesn't look like it is teaching programming --
 it should be more
 like playing with the computer, and having fun
 style, with the
 learning programming being a sort of side-effect.
 
 Ideally it would use python, but thats not that
 strict a requirement,
 squeak or logo might be acceptable, as well.
 (Although I'm prejudiced
 towards python, that being my favourite programming
 language.)
 
 Similarly, being a book isn't a requirement either,
 so a pdf, or an
 online tutorial would be fine as well, although a
 book would be
 better.
 
 I tried to search for such books, but I mostly found
 'now we are going
 to learn programming' types, and I would like
 something more subtle,
 and more motivating than that.
 
 
 Any suggestions?
 
 -- 
 Abel Daniel
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] about assert

2006-09-13 Thread Marc Poulin

--- linda.s [EMAIL PROTECTED] wrote:

 Python manual has a very brief introduction of
 assert statements. It
 is very difficult for me to understand it.


Every program has some fundamental assumptions that
must remain true in order for the program to continue
giving correct results.

The assert statement is used to verify those
assumptions. (The optional 2nd parameter can be used
to give additional information about what went wrong.)

For example, in my world no one is allowed to have a
negative age. A negative age means my program is
hopelessly confused and should halt immediately.

 myAge=42
 assert myAge=0  ## this is OK

 myAge= -1## logically impossible
 assert myAge = 0
Traceback (most recent call last):
  File interactive input, line 1, in ?
AssertionError

## here I print the condition that failed
 assert myAge =0, 'myAge = 0'
Traceback (most recent call last):
  File interactive input, line 1, in ?
AssertionError: myAge = 0


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When am I ever going to use this?

2006-08-01 Thread Marc Poulin
--- Christopher Spears [EMAIL PROTECTED] wrote:

 I've been working through a tutorial:
 http://www.ibiblio.org/obp/thinkCSpy/index.htm. 
 Lately, I have been learning about abstract data
 types
 (linked lists, stacks, queues, trees, etc.).  While
 I
 do enjoy the challenge of creating these objects, I
 am
 not sure what they are used for.
 

You probably use a linked list every day and don't
even know it.

Do you ever hit the Back button on your web browser
to return to previous pages? The browser keeps a list
of the pages you've visited, all linked together, so
you can move backwards and forwards through the list.

Here is a great resource for learning about different
kinds of data structures:

http://www.nist.gov/dads

Regards,
Marc

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] no loops

2006-07-11 Thread Marc Poulin

--- John Fouhy [EMAIL PROTECTED] wrote:

 On 12/07/06, Christopher Spears
 [EMAIL PROTECTED] wrote:
  Now the exercise is:
  As an exercise, rewrite this function so that it
  doesn't contain any loops.
 
  I have been staring at this function and drawing a
  blank.  Something tells me that I need to use
  iteration, but I am not sure how I could implement
 it.
 
 Hi Chris,
 
 You are using iteration.  That's what loops are :-)
 
 Perhaps you meant to say recursion, which is where
 a function calls
 itself.  You could solve this recursively, but I
 think Gregor's
 comment is closer to what they want you to do.
 
 -- 
 John.

I agree with Gregor and John. What makes the problem
difficult is the fact that time is represented using 3
different units of measure: hours, minutes, and
seconds. The math becomes much simpler if you convert
the time value to a single unit (such as seconds).

But it doesn't have to be seconds. I recall seeing one
 database that stores time as fractional hours where a
minute is worth 1/60 of an hour and a second is worth
1/3600 of an hour. 

In other words, 1:15 is stored as 1.25 hours, 4:30 is
stored as 4.5 hours, and so forth. Converting from
(hours, minutes, seconds) to fractional hours is
pretty easy, but going the other way is not so simple.




__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need Help

2006-07-11 Thread Marc Poulin

--- Terry Carroll [EMAIL PROTECTED] wrote:

 On Tue, 11 Jul 2006, Michael P. Reilly wrote:
 
Another aspect of your assignment will be to be
 able to use this
  functionality from the command-line.  This
 means: without asking questions
  from the user, but input is being passed as
 arguments.  
 
 John should get clarification from his instructor,
 but I did not read it 
 that way.  I read that requirement to support a
 command line query as 
 meaning the program should prompt the user to enter
 a query command; as 
 opposed to hard-coding the transactions in the coe
 itself (as is sometimes 
 done at an intro level).
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 

This may be the most important lesson to learn from
this exercise: even experienced programmers can (and
do) interpret requirements differently. When in doubt,
ask!



__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Logical Sorting

2006-07-07 Thread Marc Poulin
I did a Google search for python numeric sort and
found

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/135435

It seems to do what you want.

Marc

--- Evan Klitzke [EMAIL PROTECTED] wrote:

 Hi,
 
 I am trying to sort a list of directories that
 correspond to kernel
 sources under /usr/src/linux.  I wrote some code
 that gets a list like
 this:
 ['linux-2.6.9-gentoo-r4',
 'linux-2.6.16-gentoo-r11/',
 'linux-2.6.16-gentoo-r7/']
 
 When I sort the list, I want it to go from oldest
 (lowest version) to
 newest, so the sorted list should look like this:
 ['linux-2.6.9-gentoo-r4', 'linux-2.6.16-gentoo-r7/',
 'linux-2.6.16-gentoo-r11/']
 
 The problem is that since the built in string
 comparisons compare
 character by character, so the sort puts 2.6.16
 before 2.6.9, and -r11
 before -r7.  This is obviously not what I want.  My
 question is: are
 there any modules or built in methods that will do a
 logical sort on a
 list like this, and sort it the way I want, or will
 I have to write my
 own sorting function?
 
 -- Evan Klitzke
 
 P.S. I know that the simplest way is just to use ls
 to sort by time,
 but it is not necessarily true that older kernel
 versions have an
 older time stamp :-)
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] counting number of inputs (EARLIER VERSION SENT ACCIDENTLY)

2006-05-03 Thread Marc Poulin

Michelle:

Are you familiar with writing functions?
Here I've created a function named getInputs.

I've also created a few test cases to verify that (a)
my understanding of the problem is correct, and (b) my
solution is correct.

It's important to think about how your program is
supposed to behave in different situations. Do you
think these 3 tests are enough to prove that the code
is correct?

#
## start of code   ##
#
def getInputs():

Description:
   Collect numbers entered by the user (up to a
maximum of 5 values) and
   store them in the listOfValues.

   Stop collecting numbers if the user enters -1
or if 5 numbers have been collected.

   If the user entered -1, the -1 is NOT returned
as part of the list.   

listOfValues = [] ## this list holds the values
entered by the user

for i in range(5):
newValue = int(raw_input('Enter a number [-1
to exit]:'))
if newValue == -1:
# Return right now with whatever is
currently in the list.
return listOfValues
else:
# Add this new value to the list and keep
looping.
listOfValues.append(newValue)

## If we got this far, it means the user did not
enter a -1 so
## the list contains 5 values.
return listOfValues


Here are a few test cases to verify the logic of my
code.

Test Case 1:
   INPUTS:
  first entered value: -1
   RESULT:
  function returns empty list

Test Case 2:
   INPUTS:
  first entered value: 1
  second entered value: 2
  third entered value: -1
   RESULT:
  returned list contains [1,2]

Test Case 3:
   INPUTS:
  first entered value: 1
  second entered value: 2
  third entered value: 3
  fourth entered value: 4
  fifth entered value: 5
   RESULT:
  returned list contains [1,2,3,4,5]

if __name__ == __main__:
print getInputs()

###
## end of code   ##
###


--- Python [EMAIL PROTECTED] wrote:

 On Wed, 2006-05-03 at 15:33 -0400, MICHELLE EVANS
 wrote:
  OK, I've tried a different approach to this.
  How do I get this to stop by using -1?
  I do not want this to print until either 5 inputs
 have been entered or -1
  has been entered.  See below:
  
 
 use a for block rather than a while block to
 have a normal limit of
 5 repetitions:
 
 for x in range(5):
 
 will repeat 5 times with x running from 0 to 4.
 x is ignored - unless some use for it does turn up.
 
 the break statement allows you to terminate a block,
 so
 
   if number == -1: break
 
 will end the for block.
 
 
 Now, one of the cute features in Python is the else
 clause that goes
 with the for and while blocks.  The else block is
 executed when there is
 no break.  So the skeleton for your program can look
 something like
 
 for x in range(5):
   # get inputs and break on -1
 else:
   # no break so just process the inputs
 
 Good luck.
 
  # Add number of per hour
  numbers = []
  stop = None
  while stop != -1:
  number = int(raw_input(Run number(-1 to end)
 : ))
  numbers.append(number)
  print
  for number in numbers:
  print number
  
  
  
  
  - Original Message - 
  From: Python [EMAIL PROTECTED]
  To: MICHELLE EVANS [EMAIL PROTECTED]
  Cc: Tutor Python tutor@python.org
  Sent: Wednesday, May 03, 2006 12:18 PM
  Subject: Re: [Tutor] counting number of inputs
 (EARLIER VERSION SENT
  ACCIDENTLY)
  
  
   (Tip: Best to use reply-to-all when responding
 to an email on the list)
   On Tue, 2006-05-02 at 21:34 -0400, MICHELLE
 EVANS wrote:
number1 = int(raw_input(Run number 1 (-1 to
 end) : ))
number2 = int(raw_input(Run number 2 (-1 to
 end) : ))
number3 = int(raw_input(Run number 3 (-1 to
 end) : ))
number4 = int(raw_input(Run number 4 (-1 to
 end) : ))
number5 = int(raw_input(Run number 5 (-1 to
 end) : ))
   Good.  You collect the string from raw_input and
 convert it to an
   integer.
  
   This will prompt for 5 inputs, but it is missing
 any logic to actually
   break if -1 is entered.  With a language like
 BASIC, you could stick in
   tests sort of like:
   if number1 == -1 goto done:
   BUT Python does not have a goto.  So we actually
 need some flow
   control around the block of code where you
 collect inputs.
  
   while blocks process an indefinite number of
 times while a test
   condition is True.
  
   for blocks iterate through a sequence until they
 reach the end.  By
   providing a sequence with the correct count, you
 can repeat the block
   the correct number of times.  The range (and
 xrange for big sequences)
   functions provide a sequence of integers that
 can be used conveniently
   with for.
  
   The easiest way to fix your code above would be
 something like:
   ask_for_number = True
   while ask_for_number:
   number1 = 
   if number1 == -1: break
   ...
   number5 = ...
   ask_for_number = False
  
   HOWEVER,