Re: [Tutor] largest and smallest numbers

2007-09-25 Thread John Fouhy
You've got upper and lower bounds - maybe you could do a binary search
to find the max exactly? It should only take the same number of steps
again...

On 9/25/07, Terry Carroll [EMAIL PROTECTED] wrote:
 On Mon, 24 Sep 2007, Christopher Spears wrote:

  How can I find the largest float and complex numbers?

 That's an interesting question..

 I just tried this:

 x = 2.0
 while True:
 x = x*2
 print x
 if repr(x) == 1.#INF: break

 to just keep doubling X until Python began representing it as infinity.
 My output:

 4.0
 8.0
 16.0
 32.0
 64.0
 128.0
  . . .
 137438953472.0
 274877906944.0
 549755813888.0
 1.09951162778e+012
 2.1990232e+012
 4.3980465111e+012
  . . .
 2.24711641858e+307
 4.49423283716e+307
 8.98846567431e+307
 1.#INF

 So I'd say, the answer is somewhere between 8.98846567431e+307 and double
 that.

 On complex numbers, I'm not so sure.  My math is rusty. Is there a concept
 of greater than or largest in complex numbers on different axis?
 Which is larger, 4+2i or 2+4i?

  complex(4,2)
 (4+2j)
  complex(2,4)
 (2+4j)
  complex(4,2)  complex(2,4)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: no ordering relation is defined for complex numbers

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-25 Thread bhaaluu
On 9/25/07, Chris [EMAIL PROTECTED] wrote:
 I have this GUESSING GAME code found below:
 -
 import random
 number = random.randint(1, 101)
 print I've thought of a number between 1 and 100.
 print Try and guess it!
 print
 guess = input( What's your guess? )
 while guess != number:
 if guess  number:
 print Your guess is too high.
 else: #elif guess  number:
 print Your guess is too low.
 guess = input( What's your next guess?  )
 print Congratulations! You guessed the number.
 
 What do I have to do to the code so that the Guessing Game that tries to
 guess a number the user has thought of. (Make sure it can tell if the user
 tries to cheat, e.g. by changing the number during the game??

 I hope you can help!
 Chris

I hope so too.
You can have the user enter the secret number at the beginning of
the game, and the computer can check it's guess against the secret
number. If the computer's guess is higher than the number, the user
can reply with  or Lower. If the computer's guess is lower than
the secret number, the user can give the computer the hint of 
or Higher, whatever. But since the secret number is stored in a
variable at the beginning of the game, the user can't 'cheat' by
changing the number the computer is trying to guess, in the middle
of the game. Just have a test that checks the computer's guess
against the number stored in the secret number variable.

I recently ran across this problem while working through the Chapter 3
Challenges in Michael Dawson's _Programming Python for the Absolute
Beginner, Second Edition_. Challenge #4 :  Here's a bigger challenge.
Write the pseudocode for a program where the player and the computer
trade places in the number guessing game. That is, the player picks a
random number between 1 and 100 that the computer has to guess.
Before you start, think about how you guess. If all goes well, try coding
the game.

So my question for you, is:
Where is your pseudocode?  The challenge is to plan the game beforehand
using pseudocode. Have you thought about how you guess when you play
the Guess-A-Number game?

Show us your pseudocode for the game.
-- 
bhaaluu at gmail dot com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm. (Terry Carroll)

2007-09-25 Thread Carnell, James E
Terry,

I liked your answer!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] largest and smallest numbers (Linpeiheng)

2007-09-25 Thread 林培恒
On Mon, 24 Sep 2007, Terry Carroll write:

My math is rusty. Is there a concept of greater than 
or largest in complex numbers on different axis? Which
is larger, 4+2i or 2+4i?

In fact, complex numbers can not compare directly. People always compare 
complex numbers with their 'model'. For example, sqrt(a**2 + b**2) is the 
'model' of a+bi.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] largest and smallest numbers

2007-09-25 Thread Terry Carroll
On Tue, 25 Sep 2007, John Fouhy wrote:

 You've got upper and lower bounds - maybe you could do a binary search
 to find the max exactly? It should only take the same number of steps
 again...

I thought of that; and then I thought I'd rather go home and have dinner.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm.

2007-09-25 Thread Terry Carroll
On Tue, 25 Sep 2007, Ian Witham wrote:

 As I was using a list comprehension I wasn't sure how to make the
 calculations stop when the result of integer division == 0.

I don't see how to do that, either.  Someone on this list (sorry, I forget 
who) once suggested that the list comprehension should support a while 
predicate, similar to the if filter.  Such a predicate would not just 
filter the generated list, but actually stop its computation.

I think that would be a great idea, and this is a good use case why.  
Absent that capability, your only alternatives that I see are either 1) 
use the if-filter to filter out the list entries beyond those needed; or 
2) force the programmer to work backwards to figure out when that 
while-condition would be hit, and bake that into something like a range 
(as you did).

The downside of #1 is the computational expense: you generate a lot of
potential list enties, just to throw them away.  The downside of #2 is
that it's programmer-intensive, wasting programmer time, and prone ro
errors, as you discovered.

Now that you've solved your code, here's the function I came up with.  As 
I said, it resists implimentation as a list comprehension:

def numfaczeroes(n):

return the count of trailing zeroes from n!
e.g., 10! = 3628800; count of trailing zeros = 2

exponent = 1
fivecount = 0
while (n//(5**exponent))  0:
fivecount += n//(5**exponent)
exponent += 1
return fivecount

But what I like about is is that the only math is integer division and 
addition (and all the addition is simple incrementation).  No use of 
logarithms, not even any multiplication (which is just perverse, when you 
think about the fact that it's analyzing factorials, which are pure 
multiplication).

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm.

2007-09-25 Thread Kent Johnson
Terry Carroll wrote:
 On Tue, 25 Sep 2007, Ian Witham wrote:
 
 As I was using a list comprehension I wasn't sure how to make the
 calculations stop when the result of integer division == 0.
 
 I don't see how to do that, either.  Someone on this list (sorry, I forget 
 who) once suggested that the list comprehension should support a while 
 predicate, similar to the if filter.  Such a predicate would not just 
 filter the generated list, but actually stop its computation.

Take a look at itertools.takewhile()

 Now that you've solved your code, here's the function I came up with.  As 
 I said, it resists implimentation as a list comprehension:
 
 def numfaczeroes(n):
 
 return the count of trailing zeroes from n!
 e.g., 10! = 3628800; count of trailing zeros = 2
 
 exponent = 1
 fivecount = 0
 while (n//(5**exponent))  0:
 fivecount += n//(5**exponent)
 exponent += 1
 return fivecount

Here is a version using takewhile() and a generator expression:

from itertools import count, takewhile

def numfaczeroes2(n):
 def while_(e):
 return n//(5**e)  0
 return sum(n//(5**exponent) for exponent in takewhile(while_, 
count(1)))


It is quite a bit slower, though; probably because of the extra function 
call introduced for while_():

from timeit import Timer
print min(Timer('for i in range(1, 101): numfaczeroes(i)',
 setup='from __main__ import numfaczeroes').repeat(number=1000))

print min(Timer('for i in range(1, 101): numfaczeroes2(i)',
 setup='from __main__ import numfaczeroes2').repeat(number=1000))

prints:
0.14363694191
0.412271022797

You could also encapsulate the condition in a generator function:

def numfaczeroes3(n):
 def gen(n):
 e = 1
 while (n//(5**e))  0:
 yield e
 e += 1

 return sum(n//(5**exponent) for exponent in gen(n))

This is faster than numfaczeroes2 but still not as fast as the original.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] data type conversion for print statement

2007-09-25 Thread Tim
Hello,
I have a print statement where I use concatenation of variables with + to
avoid extra whitespaces. The variables are mixed (float/int).

How can I convert them all to strings to have a clean print statement?

example
print str(var1)+and this +str(var2)+needs to check +str(var3)

how can I convert var1, var2, var3 all at once?

This would avoid errors because of mixed data types.

BTW, why does a statment like
print var1, var2
automatically add spaces between the variables?

Thanks in advance for your help.

Timmie

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] data type conversion for print statement

2007-09-25 Thread Tom Tucker
Excerpt from an email Danny Yoo sent to me and the list in 2005.  I had the
same question. ;-)


Hi Tom,

The 'print' statement is hardcoded to add a space between elements.
print is meant to make output easy, at the cost of control.


If we need more fine-grained control over output, we may want to take a
look at the sys.stdout object; it's a file object that corresponds to the
output we send to the user.

###
 import sys
 sys.stdout
open file 'stdout', mode 'w' at 0x2a060
###

As a file object, we can use the methods that files provide:

   http://www.python.org/doc/lib/bltin-file-objects.html

But the one we'll probably want is 'write()': the write() method of a file
object lets us send content out, without any adulteration:

##
 import sys
 for i in range(5):
... sys.stdout.write('hello' + str(i))
...
hello0hello1hello2hello3hello4
##


We might have to be a little bit more careful with write(), because unlike
the print statement, the write() method isn't magical, and won't
automatically try to coerse objects to strings.  The code above shows that
we str() each number, and for good reason.  If we try without it, we'll
get a TypeError:

##
 sys.stdout.write(42)
Traceback (most recent call last):
 File stdin, line 1, in ?
TypeError: argument 1 must be string or read-only character buffer, not
int
##

So it just means we'll have to be more aware about the type of a value
when we use write().


For some more information, see:

   http://www.python.org/doc/tut/node9.html#SECTION00920
   http://www.python.org/doc/tut/node9.html#SECTION00910


Please feel free to ask more questions.  Good luck!




On 9/25/07, Tim [EMAIL PROTECTED] wrote:

 Hello,
 I have a print statement where I use concatenation of variables with +
 to
 avoid extra whitespaces. The variables are mixed (float/int).

 How can I convert them all to strings to have a clean print statement?

 example
 print str(var1)+and this +str(var2)+needs to check +str(var3)

 how can I convert var1, var2, var3 all at once?

 This would avoid errors because of mixed data types.

 BTW, why does a statment like
 print var1, var2
 automatically add spaces between the variables?

 Thanks in advance for your help.

 Timmie

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] data type conversion for print statement

2007-09-25 Thread Kent Johnson
Tim wrote:
 Hello,
 I have a print statement where I use concatenation of variables with + to
 avoid extra whitespaces. The variables are mixed (float/int).
 
 How can I convert them all to strings to have a clean print statement?
 
 example
 print str(var1)+and this +str(var2)+needs to check +str(var3)
 
 how can I convert var1, var2, var3 all at once?

Use string formatting:
print '%sand this %s needs to check %s' % (var1, var2, var3)

The %s format calls str() for its parameter.

 BTW, why does a statment like
 print var1, var2
 automatically add spaces between the variables?

Because it is convenient. Use string formatting to get closer control.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Take if offline

2007-09-25 Thread Hansen, Mike
Anytime someone posts in HTML, or posts without a subject, or
accidentally
hijacks a thread, or top-posts, or writes in caps, a couple of posters
pop up
and complain. Rather than posting to the entire list, I think it'd be
best if
you send your complaint directly to the offending user. I'd prefer to
read
about Python not read lessons in net/mail-list etiquette. 

Thanks,

Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



Re: [Tutor] Take if offline

2007-09-25 Thread Darren Williams
So by your own rules, you should have sent that to the offending user(s).

- Original Message - 
From: Hansen, Mike [EMAIL PROTECTED]
To: python tutor tutor@python.org
Sent: Tuesday, September 25, 2007 2:27 PM
Subject: [Tutor] Take if offline


 Anytime someone posts in HTML, or posts without a subject, or
 accidentally
 hijacks a thread, or top-posts, or writes in caps, a couple of posters
 pop up
 and complain. Rather than posting to the entire list, I think it'd be
 best if
 you send your complaint directly to the offending user. I'd prefer to
 read
 about Python not read lessons in net/mail-list etiquette. 
 
 Thanks,
 
 Mike
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] largest and smallest numbers (Linpeiheng)

2007-09-25 Thread Noufal Ibrahim
林培恒 wrote:
 On Mon, 24 Sep 2007, Terry Carroll write:
 
 My math is rusty. Is there a concept of greater than 
 or largest in complex numbers on different axis? Which
 is larger, 4+2i or 2+4i?
 
 In fact, complex numbers can not compare directly. People always compare 
 complex numbers with their 'model'. For example, sqrt(a**2 + b**2) is the 
 'model' of a+bi.

i think you mean modulus. Also, you can get it directly. The abs 
builtin does it for you.
  foo = 3+4j
  print abs(foo)

I don't think there is a direct way to the get the argument. You'd have 
to use arctan for that.


-- 
~noufal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Take if offline

2007-09-25 Thread Kent Johnson
Hansen, Mike wrote:
 Anytime someone posts in HTML, or posts without a subject, or
 accidentally
 hijacks a thread, or top-posts, or writes in caps, a couple of posters
 pop up
 and complain. Rather than posting to the entire list, I think it'd be
 best if
 you send your complaint directly to the offending user. I'd prefer to
 read
 about Python not read lessons in net/mail-list etiquette. 

Hmmm. So instead, whenever someone complains about netiquette on-list we 
should post on-list complaining about it? I'm not sure that improves the 
situation any, especially if it sparks a discussion. Perhaps you should 
email your suggestion privately to the offending parties. :-)

I definitely think this list is about how to be part of the Python 
community, as well as how to program in Python. Knowing how to 
participate in a mailing list is part of that. Maybe you could just skip 
those posts, there are not that many of them.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] data type conversion for print statement

2007-09-25 Thread Noufal Ibrahim
Tim wrote:
 Hello,
 I have a print statement where I use concatenation of variables with + to
 avoid extra whitespaces. The variables are mixed (float/int).
 
 How can I convert them all to strings to have a clean print statement?
 
 example
 print str(var1)+and this +str(var2)+needs to check +str(var3)

Well, if they're all ints or floats, you could do something like
print %fand this %fneeds to check %f%(val1,val2,val3) but you'll get 
decimal points for everything.

I suppose you could also do but it's a little less readable
print %sand this %sneeds to check %s%tuple([str(x) for x in 
(val1,val2,val3)])

Both don't look very pythonic to me though. :(


-- 
~noufal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Take if offline

2007-09-25 Thread Michael Langford
I agree with Kent...

-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com

On 9/25/07, Kent Johnson [EMAIL PROTECTED] wrote:

 Hansen, Mike wrote:
  Anytime someone posts in HTML, or posts without a subject, or
  accidentally
  hijacks a thread, or top-posts, or writes in caps, a couple of posters
  pop up
  and complain. Rather than posting to the entire list, I think it'd be
  best if
  you send your complaint directly to the offending user. I'd prefer to
  read
  about Python not read lessons in net/mail-list etiquette.

 Hmmm. So instead, whenever someone complains about netiquette on-list we
 should post on-list complaining about it? I'm not sure that improves the
 situation any, especially if it sparks a discussion. Perhaps you should
 email your suggestion privately to the offending parties. :-)

 I definitely think this list is about how to be part of the Python
 community, as well as how to program in Python. Knowing how to
 participate in a mailing list is part of that. Maybe you could just skip
 those posts, there are not that many of them.

 Kent
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] data type conversion for print statement

2007-09-25 Thread Kent Johnson
Noufal Ibrahim wrote:

 I suppose you could also do but it's a little less readable
 print %sand this %sneeds to check %s%tuple([str(x) for x in 
 (val1,val2,val3)])

The %s formatter takes care of the string conversion, the list 
comprehension is not needed. Just use

print %sand this %sneeds to check %s % (val1,val2,val3)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Take if offline

2007-09-25 Thread Darren Williams
All jokes aside now (glad you took it that way Mike).  Maybe I havn't gave 
the tutor enough attention, but I have never witnessed someone jump down 
anothers throat because they posted in all caps, didn't enter a subject 
etc... etc...  The one time I have seen an argument (well heated debate) was 
about a subject entitled 'Losing the expressiveness of C' (or something 
similar), which I found pretty interesting.

- Original Message - 
From: Shawn Milochik [EMAIL PROTECTED]
To: Darren Williams [EMAIL PROTECTED]
Sent: Tuesday, September 25, 2007 3:06 PM
Subject: Re: [Tutor] Take if offline


 No, not really. He had to give everyone the rule once. Otherwise, he'd
 have to do it a hundred times a day, and monitor every single post to
 find out who he had to inform. He'd end up doing not much else with
 his life, and would flee to a monastery and give up coding forever.
 You wouldn't want that to happen, would you?



 On 9/25/07, Darren Williams [EMAIL PROTECTED] wrote:
 So by your own rules, you should have sent that to the offending user(s).

 - Original Message -
 From: Hansen, Mike [EMAIL PROTECTED]
 To: python tutor tutor@python.org
 Sent: Tuesday, September 25, 2007 2:27 PM
 Subject: [Tutor] Take if offline


  Anytime someone posts in HTML, or posts without a subject, or
  accidentally
  hijacks a thread, or top-posts, or writes in caps, a couple of posters
  pop up
  and complain. Rather than posting to the entire list, I think it'd be
  best if
  you send your complaint directly to the offending user. I'd prefer to
  read
  about Python not read lessons in net/mail-list etiquette.
 
  Thanks,
 
  Mike
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor



 -- 
 Please read:
 http://milocast.com/2007/07/31/this-i-believe/
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm.

2007-09-25 Thread Ian Witham


 from itertools import count, takewhile

 def numfaczeroes2(n):
  def while_(e):
  return n//(5**e)  0
  return sum(n//(5**exponent) for exponent in takewhile(while_,
 count(1)))


 It is quite a bit slower, though; probably because of the extra function
 call introduced for while_():


I sped this up slightly by using a lambda function instead of while_(), but
not by much.

My solution still took over 5 seconds on the Sphere Judge machine. The top
Python solution takes around 0.6 seconds!

While I haven't submitted Terry's solution, my Timer comparisons suggest
that it would take around 1.5 seconds to process the data. This doesn't take
into account any time used inputting and outputting the test data.

If anyone knows the 0.6 second solution feel free to share!

Ian.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm.

2007-09-25 Thread Terry Carroll
On Wed, 26 Sep 2007, Ian Witham wrote:

 My solution still took over 5 seconds on the Sphere Judge machine.

How much data are they throwing at you?  For the sample data they provide 
on the website, your first slow solution finished on my machine almost 
instantaneously.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm.

2007-09-25 Thread Ian Witham
On 9/26/07, Terry Carroll [EMAIL PROTECTED] wrote:

 On Wed, 26 Sep 2007, Ian Witham wrote:

  My solution still took over 5 seconds on the Sphere Judge machine.

 How much data are they throwing at you?  For the sample data they provide
 on the website, your first slow solution finished on my machine almost
 instantaneously.


Approximately 10 numbers in the range 1 = N = 10, and they run
your solution on a Pentium III 700MHz.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help speeding up algorithm.

2007-09-25 Thread Ian Witham
On 9/26/07, Terry Carroll [EMAIL PROTECTED] wrote:

 On Tue, 25 Sep 2007, Ian Witham wrote:

 def numfaczeroes(n):
 
 return the count of trailing zeroes from n!
 e.g., 10! = 3628800; count of trailing zeros = 2
 
 exponent = 1
 fivecount = 0
 while (n//(5**exponent))  0:
 fivecount += n//(5**exponent)
 exponent += 1
 return fivecount


I've come up with a couple of improvements to your function

First I moved the (n//(5**exponent)) out of the while definition as it was
being evaluated twice for each iteration:

def numfaczeroes(n):

return the count of trailing zeroes from n!
e.g., 10! = 3628800; count of trailing zeros = 2

exponent = 1
fivecount = 0
count = (n//(5**exponent))

while count  0:
fivecount += count
exponent += 1
count = (n//(5**exponent))
return fivecount

This shaved about 10% off the run time.

Then I got rid of the (n//(5**exponent)) altogether:

def numfaczeroes(n):

return the count of trailing zeroes from n!
e.g., 10! = 3628800; count of trailing zeros = 2


fivecount = 0
count = n//5

while count  0:
fivecount += count
count /= 5
return fivecount

This shaved a further 40% off the run time!

In practice, the Sphere Judge still takes 3 seconds to execute. So perhaps
there is another bottleneck in my program -- maybe the input/output?

Interestingly, print runs faster than sys.stout.write, but input() is far
slower than sys.stdin.readline

import sys

for test in range(int(sys.stdin.readline())):

n = int(sys.stdin.readline())

fivecount = 0
count = n/5

while count  0:
fivecount += count
count /= 5

print fivecount
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pickle question

2007-09-25 Thread Jeff Peery
hello, 
   
  I have a question about the pickler. I'm using it to save objects in a 
program of mine that I can later reload to use. I was wondering how the pickle 
works and how it references the class module when I unpickle the pickled 
objects. for example I save some objects using the pickler, then I continue to 
develop my program so the class module is changed and additional attributes and 
methods are added. What happens now when I unpickle the pickled data and try to 
operate on it using the new methods and new attributes?
   
  here's an example if I didn't explain myself well...
   
  I create a class of data called 'var'
   
  class var:
def __init__(self):
self.data = numpy.array([1,2,3,4])
  self.color = 'blue'
   
  def operate(self):
return self.data+1
   
  now create the instance: 
  myVariable = var()
  I then pickle it to some file. and now I continue to develop my class module:
   
  class var:
def __init__(self):
self.data = numpy.array([1,2,3,4])
  self.color = 'blue'
  self.name = 'bob'
   
  def operate(self):
return self.data+1
   
  def GetName(self):
return self.name
   
  So now I unpickle my object from before using my new program. and I use 
MyVar.GetName(). does this work? If not, how do people usually handle this type 
of problem? or do they not use the pickler to save program data?
   
  big thanks!
   
  Jeff

   
-
Fussy? Opinionated? Impossible to please? Perfect.  Join Yahoo!'s user panel 
and lay it on us.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] data type conversion for print statement

2007-09-25 Thread O.R.Senthil Kumaran
 
 The 'print' statement is hardcoded to add a space between elements.
 print is meant to make output easy, at the cost of control.

Well, that was a good example. I had prepared Notes for myself also along the 
same lines.

print and softspace in python

In python, whenever you use print statement it will append a newline by 
default. If you don't want newline to be appended, you got use a comma at the 
end (print 10,)
When, you have a list of characters and want them to be printed together a 
string using a for loop, there was observation that no matter what there was 
space coming between the characters. No split or  join methods helped.
list1=['a','b','c']
for e in list1:
   print e,
a b c
# Without whitespace it will look like.
print abc
abc

The language reference says that print is designed to output a space before any 
object. And some search goes to find and that is controlled by softspace 
attribute of sys.stdout.
Way to print without leading space is using sys.stdout.write()

import sys
for e in list1:
  sys.stdout.write(e)
abc

Reference manual says:

A space is written before each object is (converted and) written, unless the 
output system believes it is positioned at the beginning of a line. This is the 
case (1) when no characters have yet been written to standard output, (2) when 
the last character written to standard output is \n, or (3) when the last 
write operation on standard output was not a print statement. (In some cases it 
may be functional to write an empty string to standard output for this reason.)
Not getting the last part as how you will write  a empty string and use print  
not appending  blank space in a single line

http://phoe6.livejournal.com/50886.html

-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Take if offline

2007-09-25 Thread Hansen, Mike
h!!! You posted in HTML and top posted! OH NO! I top-posted too!


 runs away screaming and bouncing off the walls like Daffy Duck  

  _   

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Fiyawerx 
Sent: Tuesday, September 25, 2007 6:45 PM 
To: Darren Williams 
Cc: tutor@python.org; Shawn Milochik 
Subject: Re: [Tutor] Take if offline 


It also seems, for people like me, I've learned a lot of 'what not to
do' by seeing peoples replies to others posts telling them.. well, what
not to do. If all these were taken 'off list' you'd have a whole lot
more people doing bad things because they wouldn't have seen yet not to
do it, not to mention a whole lot more people having to send 'off-list'
complaints to people, due to the same reason. 


On 9/25/07, Darren Williams HYPERLINK
mailto:[EMAIL PROTECTED][EMAIL PROTECTED] wrote: 

All jokes aside now (glad you took it that way Mike).  Maybe I havn't
gave 
the tutor enough attention, but I have never witnessed someone jump down

anothers throat because they posted in all caps, didn't enter a subject 
etc... etc...  The one time I have seen an argument (well heated debate)
was 
about a subject entitled 'Losing the expressiveness of C' (or something 
similar), which I found pretty interesting. 

- Original Message - 
From: Shawn Milochik HYPERLINK
mailto:[EMAIL PROTECTED][EMAIL PROTECTED] 
To: Darren Williams HYPERLINK
mailto:[EMAIL PROTECTED][EMAIL PROTECTED] 
Sent: Tuesday, September 25, 2007 3:06 PM 
Subject: Re: [Tutor] Take if offline 


 No, not really. He had to give everyone the rule once. Otherwise, he'd

 have to do it a hundred times a day, and monitor every single post to 
 find out who he had to inform. He'd end up doing not much else with 
 his life, and would flee to a monastery and give up coding forever. 
 You wouldn't want that to happen, would you? 
 
 
 
 On 9/25/07, Darren Williams HYPERLINK mailto:[EMAIL PROTECTED]
[EMAIL PROTECTED] wrote: 
 So by your own rules, you should have sent that to the offending
user(s). 
 
 - Original Message - 
 From: Hansen, Mike HYPERLINK mailto:[EMAIL PROTECTED]
[EMAIL PROTECTED] 
 To: python tutor HYPERLINK
mailto:tutor@python.orgtutor@python.org 
 Sent: Tuesday, September 25, 2007 2:27 PM 
 Subject: [Tutor] Take if offline 
 
 
  Anytime someone posts in HTML, or posts without a subject, or 
  accidentally 
  hijacks a thread, or top-posts, or writes in caps, a couple of
posters 
  pop up 
  and complain. Rather than posting to the entire list, I think it'd
be 
  best if 
  you send your complaint directly to the offending user. I'd
prefer to 
  read 
  about Python not read lessons in net/mail-list etiquette. 
  
  Thanks, 
  
  Mike 
  ___ 
  Tutor maillist  -  HYPERLINK
mailto:Tutor@python.orgTutor@python.org 
  http://mail.python.org/mailman/listinfo/tutor 
  
 ___ 
 Tutor maillist  -  HYPERLINK
mailto:Tutor@python.orgTutor@python.org 
 HYPERLINK
http://mail.python.org/mailman/listinfo/tutorhttp://mail.python.org/ma
ilman/listinfo/tutor 
 
 
 
 -- 
 Please read: 
 http://milocast.com/2007/07/31/this-i-believe/ 
 

___ 
Tutor maillist  -  HYPERLINK mailto:Tutor@python.orgTutor@python.org 
http://mail.python.org/mailman/listinfo/tutor 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] home_finance.py

2007-09-25 Thread Christopher Spears
I'm working on a problem in Chapter 5 of Core Python
Programming(2nd Edition).  I am supposed to write a
script that take an opening balance and a monthly
payment from the user.  The script then displays the
balance and payments like so:


Enter opening balance: 100
Enter monthly payment: 16.13
Amount  Remaining
Pymnt#  PaidBalance
0   0.00100.00
1   16.13   83.87
2   16.13   67.74
3   16.13   51.61
4   16.13   35.48
5   16.13   19.35
6   16.13   3.22
73.22   0.00

Here is what I have written so far:

#!/usr/bin/env python

balance = float(raw_input(Enter opening balance: ))
payment = float(raw_input(Enter monthly payment: ))

print \tAmount\tRemaining
print Pymnt#\tPaid\tBalance

payment_num = 0
print %d\t%.2f\t%.2f % (payment_num,0,balance)

while balance = 0:
payment_num = payment_num + 1
if balance  0:
balance = balance - payment
print %d\t%.2f\t%.2f %
(payment_num,payment,balance)
else:
payment = balance
balance = balance - payment
print %d\t%.2f\t%.2f %
(payment_num,payment,balance)

This is what my script displays:

Enter opening balance: 100
Enter monthly payment: 16.13
Amount  Remaining
Pymnt#  PaidBalance
0   0.00100.00
1   16.13   83.87
2   16.13   67.74
3   16.13   51.61
4   16.13   35.48
5   16.13   19.35
6   16.13   3.22
7   16.13   -12.91

I'm not sure how to solve this problem.  Apparently,
trying to catch the remaining balance when it becomes
negative doesn't work!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-25 Thread Chris
Ok, in this case the guessing game is design to guess the number (0-100)
that the user holds in his head.

I assume that Python off the bat Python gives a first guess.

Example:

I guess 50  #(using the split range)
Depending on the user # Python asks:

Press 1 if I am high.
Press 2 if I am low.
Press 3 if I got it.

Throughout the guessing Python should use the split range method. Python
should also be able to tell the user if he or she is cheatingand you
wonder why I need help...yikes!

Pseudo code:

Guess the number from the user using 1,2,3 keys
Ask the user if # is high, low, or dead on.
Print 'you got it' when Python gets it.

As you can see I need help. I've given you the pseudo code.

Chris




-Original Message-
From: bhaaluu [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 25, 2007 4:37 AM
To: Chris
Cc: tutor@python.org
Subject: Re: [Tutor] python problem

On 9/25/07, Chris [EMAIL PROTECTED] wrote:
 I have this GUESSING GAME code found below:
 -
 import random
 number = random.randint(1, 101)
 print I've thought of a number between 1 and 100.
 print Try and guess it!
 print
 guess = input( What's your guess? )
 while guess != number:
 if guess  number:
 print Your guess is too high.
 else: #elif guess  number:
 print Your guess is too low.
 guess = input( What's your next guess?  )
 print Congratulations! You guessed the number.
 
 What do I have to do to the code so that the Guessing Game that tries to
 guess a number the user has thought of. (Make sure it can tell if the user
 tries to cheat, e.g. by changing the number during the game??

 I hope you can help!

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
3:59 PM
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle question

2007-09-25 Thread Kent Johnson
Jeff Peery wrote:
 hello,
  
 I have a question about the pickler. I'm using it to save objects in a 
 program of mine that I can later reload to use. I was wondering how the 
 pickle works and how it references the class module when I unpickle the 
 pickled objects. for example I save some objects using the pickler, then 
 I continue to develop my program so the class module is changed and 
 additional attributes and methods are added. What happens now when I 
 unpickle the pickled data and try to operate on it using the new methods 
 and new attributes?

The pickle docs suggest that the way to handle this is to write your own 
__setstate__() method and possibly to include a version number in the 
object or write one with a __getstate__() method.

In your example, I think you could have something like this (not tested):

def __setstate__(self, d):
   if 'name' not in d:
 d['name'] = 'bob' # some appropriate default for old pickles
   self.__dict__.update(d)

Every time you change the class data, add a bit of conversion code to 
__setstate__(). If you just keep accreting converters to the end of 
__setstate__(), it will be able to upgrade any version of pickle.

Kent

PS Please don't start a new thread by replying to another thread, create 
a new message. Those of use who use threaded mail readers will 
appreciate it.

PPS Mike Hansen please ignore the previous PS ;-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-25 Thread Kent Johnson
Chris wrote:

 Guess the number from the user using 1,2,3 keys
   Ask the user if # is high, low, or dead on.
   Print 'you got it' when Python gets it.
 
 As you can see I need help. I've given you the pseudo code.

OK, how about some real code? If we write the program for you that won't 
help you learn. What have you tried so far? Which parts of the 
pseudocode do you know how to code? Which parts do you need help with?

Kent



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-25 Thread Chris

  Ok as I see it, it's should go like this:

Print 'Think of a number between 1 and 100, and let me guess it'

Type1 = input ('Type 1 if I am high')
Type2 = input ('Type 2 if I am low')
Type3 = input ('Type 3 if I am dead on')

I can't seem to get the guts of it.  I assume that there are 3 if statements
and a while statement.

guess

While x != 
If guess 



I get lost right around here...help!


import random
 number = random.randint(1, 101)
 print I've thought of a number between 1 and 100.
 print Try and guess it!
 print
 guess = input( What's your guess? )
 while guess != number:
 if guess  number:
 print Your guess is too high.
 else: #elif guess  number:
 print Your guess is too low.
 guess = input( What's your next guess?  ) print 
 Congratulations! You guessed the number.
-Original Message-
From: Kent Johnson [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 25, 2007 7:32 PM
To: Chris
Cc: tutor@python.org
Subject: Re: [Tutor] python problem

Chris wrote:

 Guess the number from the user using 1,2,3 keys
   Ask the user if # is high, low, or dead on.
   Print 'you got it' when Python gets it.
 
 As you can see I need help. I've given you the pseudo code.

OK, how about some real code? If we write the program for you that won't 
help you learn. What have you tried so far? Which parts of the 
pseudocode do you know how to code? Which parts do you need help with?

Kent




No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
3:59 PM
 

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
3:59 PM
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] home_finance.py

2007-09-25 Thread Ian Witham
On 9/26/07, Christopher Spears [EMAIL PROTECTED] wrote:

 I'm working on a problem in Chapter 5 of Core Python
 Programming(2nd Edition).  I am supposed to write a
 script that take an opening balance and a monthly
 payment from the user.  The script then displays the
 balance and payments like so:


 Enter opening balance: 100
 Enter monthly payment: 16.13
 Amount  Remaining
 Pymnt#  PaidBalance
 0   0.00100.00
 1   16.13   83.87
 2   16.13   67.74
 3   16.13   51.61
 4   16.13   35.48
 5   16.13   19.35
 6   16.13   3.22
 73.22   0.00

 Here is what I have written so far:

 #!/usr/bin/env python

 balance = float(raw_input(Enter opening balance: ))
 payment = float(raw_input(Enter monthly payment: ))

 print \tAmount\tRemaining
 print Pymnt#\tPaid\tBalance

 payment_num = 0
 print %d\t%.2f\t%.2f % (payment_num,0,balance)

 while balance = 0:
 payment_num = payment_num + 1
 if balance  0:


You have: if balance  0:

You should be checking whether the remaining balance is greater than the
payment amount, not whether it is greater than 0.
This is probably just a typo as it looks like you're on the right track.


Ian
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor