Re: [Tutor] syracuse sequence (collatz or hailstone)

2005-10-30 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
> would this be a possible use of a list and appending even though I recieve
> an error from it:
> 
> def main():
>  x = [1]
>  x[0] = input('enter an int to start your syracuse sequence\n')
>  while not isinstance(x[0], int):
>  x[0] = input('no, please enter an int to start your syracuse
> sequence\n')
>  while x[-1] != 1:
>  if ((x[-1] % 2) == 0):
>  x.append(x[-1] / 2)
>  else:
>  x.append((3 * x) + 1)

This should be
  x.append((3 * x[-1]) + 1)

The error is caused by the expression ((3*x) + 1). When x is a list, 3*x is a 
valid operation; it creates a new list with the elements of the original list 
repeated three times. Then you do newlist+1. For a list, + means concatenate - 
it puts two lists together. Since the right-hand value is not a list, you get 
an error.

Other than that your code is OK but it would be clearer (and avoid errors like 
the above!) if you used separate names for the current value of x and for the 
list.

Kent

>  print len(x), x
> 
> print "The Syracuse sequence of your starting value is:", x
> 
> main()
> 
> line 10, in main
> x.append((3 * x) + 1)
> TypeError: can only concatenate list (not "int") to list
> 
> 
> 
> 
>>[EMAIL PROTECTED] wrote:
>>
>>>hello,
>>>
>>>Could I gather all of the values from print x into a string or a range?
>>>Since, I am not familiar with lists yet.
>>
>>Here is a simple example of gathering values into a list and making a
>>string:
>> >>> r=[] # Start with an empty list
>> >>> for x in range(3): # x will be 0, 1, 2 in sequence
>> ...   r.append(str(x*x)) # Put x*x (as a string) onto r
>> ...
>> >>> r
>>['0', '1', '4']
>> >>> ', '.join(r) # make a single string by joining the elements of r with
>>', '
>>'0, 1, 4'
>>
>>Kent
>>
>>
>>>
>>>def main():
>>>x = input("Please enter a positive starting value: ")
>>> while x != 1:
>>> if x%2 == 0:
>>> x = x/2
>>>else:
>>>x = x*3+1
>>>print x
>>>print "The Syracuse sequence of your starting value is:", x
>>>
>>>main()
>>>
>>>
>>>
>>>
>>>- Original Message -
>>>From: "Frank Bloeink" <[EMAIL PROTECTED]>
>>>To: <[EMAIL PROTECTED]>
>>>Sent: Friday, October 28, 2005 5:06 AM
>>>Subject: Re: [Tutor] syracuse sequence (collatz or hailstone)
>>>
>>>
>>>
>>>
Hey,

your code seems almost alright to me, except that in your case it's only
printing the last number of your sequence, which obviously is not what
you want. Quick fix would be to insert a line "print x" just below else
statement:
---snip--
else:
  x=x*3+1
print x
---snip
This should make clear where the error is: You are just calculating, but
not printing the sequence!
If you want to leave the output to the end of the program you could as
well gather all the calculated values in a list or similar structure and
then print the contents of the list..

hth Frank

On Fri, 2005-10-28 at 01:22 -0400, [EMAIL PROTECTED] wrote:


>Hello
>
>I am trying to create a program that will calculate the syracuse
>sequence
>which is also known as collatz or hailstone. the number that is input
>by
>the user may be either even or odd. the number goes through a series of
>functions which are x/2 if the number is even and 3x+1 if the number is
>odd. it keeps doing so until the number reaches 1. An example would be
>if
>the user inputed 5 they should recieve: 5, 16, 8, 4, 2, 1 as the
>sequence
>for the value that they started with. My code currently just prints a 1
>and none of the numbers that would have preceded it. any ideas on how I
>could get the program to not do this would be greatly appreciated.
>
>
>def main():
>   try:
>   x = input("Please enter a starting value: ")
>   while x != 1:
>
>   if x%2 == 0:
>   x = x/2
>   else:
>   x = x*3+1
>
>   except ValueError, excObj:
>   msg = str(excobj)
>   if msg == "math domain error":
>   print "No negatives or decimals."
>   else:
>   print "Something went wrong."
>
>
>
>   print "The Syracuse sequence of your starting value is:", x
>
>main()
>
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor



>>>___
>>>Tutor maillist  -  Tutor@python.org
>>>http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>
>>--
>>http://www.kentsjohnson.com
>>
>>___
>>Tutor maillist  -  Tutor@python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
> 
> 
> 
> 
> 

-- 
http://www.kentsjohnson.com

_

Re: [Tutor] AJAX resources for Python

2005-10-30 Thread Andrew P
Turbogears seconded.  It's a really great collection of tools.

As an alternative, you  may also want to check out Nevow.  From the webpage:

"Nevow includes LivePage, a two-way bridge between JavaScript in a
browser and Python on the server. For 0.3, LivePage has been updated
to operate on Mozilla, Firefox, Windows Internet Explorer 6, and
Safari on Mac OS X. Event handlers can be written in pure Python and
JavaScript implementation details are hidden from the programmer, with
Nevow taking care of routing data to and from the server using
XmlHttpRequest."

You'd use it with Twisted, but it may be the easiest solution once the
initial learning curve is over.  Livepage actually predates the AJAX
terminology, so it's been around a while.  The only major example I've
seen is at http://www.jotlive.com, but it's a pretty good one. 
Multiple people can edit a document interactively at the same time.  I
tried the free account, and it was impressive.

Good luck,

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


[Tutor] Transforming object into subclass instance

2005-10-30 Thread Jan Eden
Hi,

my program uses an initial "switch" statement to create an object:

valid_types = dict(
pages=Show.Page,
syndicated=Show.Syndicated,
authors=Show.Author,
author_list=Show.AuthorList,
galleries=Show.Gallery,
pictures=Show.Picture,
slideshow=Show.SlidePicture,
tex=Show.Tex,
blogs=Show.Blog,
blogentries=Show.BlogEntry,
search=Show.Search,
search_author=Show.SearchAuthor,
not_found=Show.NotFound
)
# ... code ommitted...

page = valid_types[type]]()

For a certain class, it is only during the execution of the __ini__ method that 
I can distinguish between two subtypes (subclasses). The subclasses define two 
different versions of method A.

Now my question is: How can I turn an object of class X into an object of 
either class Y or Z while (retaining all the attributes defined so far).

I know I could solve the problem by using another switch statement - but is 
there consistent OOP solution for this problem?

Thanks,

Jan
-- 
Hanlon's Razor: Never attribute to malice that which can be adequately 
explained by stupidity.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python as Application (OT now)

2005-10-30 Thread Alan Gauld
> Papers are generally poorly formatted. Things like using the space
> bar or tab key to center things. Then even when the docs do open
> up, the formatting is all askew.
> 
This is one of my pet peeves. Many so called MS Office training 
courses barely touch on the use of paragraph formats, very few 
go into them in any depth and yet they are fundamental to consistent 
documemnts easily produced. The whole concept of styles is missed 
by many users - even the IT students we employ on vacation training

> Perhaps if teachers thought more about the _abstraction_ of a word
> processor and the _abstraction_ of a spreadsheet, and focused on
> how these tools are best used  --  instead of focusing on how to
> use the specific implementations -- we would have fewer problems,
> and more capable students.

Absolutely correct. The same is true of programming however. 
Too many programming courses teach a language rather than programming.

There is no difference between a course teaching programming 
students Python and one teaching business students Excel IMHO.
Teach the principles, illustrate with a tool (or better several tools)
and you get a much more effective student.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




> 
> To me, programming is all about abstraction.
> 
> 
> Thanks for your time.
> (please follow up to edu-sig)
> 
> _
> Express yourself instantly with MSN Messenger! Download today it's FREE! 
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
> 
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with prime number program

2005-10-30 Thread andrade1
Hello

I am trying to write a program that will figure out if a number is prime
or not. Currently this is the code that I have:

import math

def main():

number=input("Please enter a positive whole number greater than 2: ")
for value in range(2, number-math.sqrt):
if number % value == 0:
print number, "is not prime"
exit
print number, "is prime"

main()

my problem is that the output prints both statements whether the number is
prime or not. How could I get it to only print the one statement saying
that 49 is not a prime number?

enter a number greater than 2: 49
49 is not prime
49 is prime



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


Re: [Tutor] help with prime number program

2005-10-30 Thread Danny Yoo


> I am trying to write a program that will figure out if a number is prime
> or not. Currently this is the code that I have:

[code cut]

Hi Andrade1,

As in your other previous homework programs, you seem to have some trouble
with control flow.  Have you considered talking with your professor or
teaching assistant about this?


> import math
>
> def main():
> number=input("Please enter a positive whole number greater than 2: ")
> for value in range(2, number-math.sqrt):
> if number % value == 0:
> print number, "is not prime"
> exit
> print number, "is prime"
>
> main()
>
> my problem is that the output prints both statements whether the number
> is prime or not.


The block of code:

##
for value in range(2, number-math.sqrt):
if number % value == 0:
print number, "is not prime"
exit
##

has two issues.

1.  What is 'exit'?

2.  What is 'number - math.sqrt'?

Instead of 'exit', you may be looking for the 'return' statement instead.


Good luck to you.

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


Re: [Tutor] Transforming object into subclass instance

2005-10-30 Thread Kent Johnson
Jan Eden wrote:
> Now my question is: How can I turn an object of class X into an
> object of either class Y or Z while (retaining all the attributes
> defined so far).

This is easy, in fact - you can change the class of an instance by assigning to 
its __class__ attribute:
 >>> class F(object):
 ...   def p(self): print "F"
 ...
 >>> class G(object):
 ...   def p(self): print "G"
 ...
 >>> f=F()
 >>> f.p()
F
 >>> type(f)

 >>> f.__class__ = G
 >>> f.p()
G
 >>> type(f)

> 
> I know I could solve the problem by using another switch statement -
> but is there consistent OOP solution for this problem?

I'm not sure I would call this solution OO or pretty but it does work.

Kent
-- 
http://www.kentsjohnson.com

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


Re: [Tutor] help with prime number program

2005-10-30 Thread andrade1
>
>
>> I am trying to write a program that will figure out if a number is prime
>> or not. Currently this is the code that I have:
>
> [code cut]
>
> Hi Andrade1,
>
> As in your other previous homework programs, you seem to have some trouble
> with control flow.  Have you considered talking with your professor or
> teaching assistant about this?
>
>
>> import math
>>
>> def main():
>> number=input("Please enter a positive whole number greater than 2:
>> ")
>> for value in range(2, number-math.sqrt):
>> if number % value == 0:
>> print number, "is not prime"
>> exit
>> print number, "is prime"
>>
>> main()
>>
>> my problem is that the output prints both statements whether the number
>> is prime or not.
>
>
> The block of code:
>
> ##
> for value in range(2, number-math.sqrt):
> if number % value == 0:
> print number, "is not prime"
> exit
> ##
>
> has two issues.
>
> 1.  What is 'exit'?
instead of exit I can use break so that the loop will stop executing

> 2.  What is 'number - math.sqrt'?
for math.sqrt I could use math.sqrt(n) so that it would take the sqrt of n
which is entered in by the user


However for the output I am still receiving

enter a number greater than 2: 49
49 is prime
49 is prime
49 is prime
49 is prime
49 is prime
49 is not prime

> Instead of 'exit', you may be looking for the 'return' statement instead.
>
>
> Good luck to you.
>
>


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


Re: [Tutor] help with prime number program

2005-10-30 Thread Adam
    > 1.  What is 'exit'?
instead of exit I can use break so that the loop will stop executing> 2.  What is 'number - 
math.sqrt'?for math.sqrt I could use math.sqrt(n) so that it would take the sqrt of nwhich is entered in by the userHowever for the output I am still receivingenter a number greater than 2: 49
49 is prime49 is prime49 is prime49 is prime49 is prime49 is not prime
The problem is using break just breaks from the loop and then goes into the next instruction being
print number, "is prime"
As Danny suggested before using return will stop the entire function rather than the just the loop.

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


[Tutor] Newbie Question - Python vs Perl

2005-10-30 Thread Scott Clausen
As a newbie to Python I'd like to know if someone can tell me some  
strengths and weaknesses of this language. The reason I'm asking is a  
friend told me I should learn Perl over Python as it's more  
prevalent.  I'm going to be using it on a Mac.

I'd appreciate hearing any views on this topic. My own view is that  
it's always good to learn new things as you then have more tools to  
use in your daily programming.

Thanks in advance.

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


Re: [Tutor] Newbie Question - Python vs Perl

2005-10-30 Thread Kent Johnson


Scott Clausen wrote:
> As a newbie to Python I'd like to know if someone can tell me some  
> strengths and weaknesses of this language. The reason I'm asking is a  
> friend told me I should learn Perl over Python as it's more  
> prevalent.  I'm going to be using it on a Mac.

http://wiki.python.org/moin/BeginnersGuide/Overview
http://www.linuxjournal.com/article/3882

You might be interested in this site that lets you compare Python and Perl code
http://pleac.sourceforge.net/

-- 
http://www.kentsjohnson.com

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


Re: [Tutor] python myspace module?

2005-10-30 Thread Andrew P
Probably the most useful library from the wwwsearch page Danny pointed
you to is the cookielib, which is built into Python as of 2.3 or 2.4. 
The most useful because you can't scrape by without it on most sites,
and cookies are really no fun to handle manually  Login and forms can
largely be fudged, but it's really nice to have cookies handled
transparently.

You'll want to do something like:

url = "http://www.myspace.com";
cj = cookielib.CookieJar()
myURLOpen = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
myURLOpen.addheaders = [('User-agent', 'Mozilla/5.0')]
urlopen = myURLOpen.open

urlopen(url).read()

I think this came straight from the Python docs originally, where it's
explained a bit more fully.  But this will do two things.  1) set up a
client side cookie, and magically handle all cookie magic for you, and
2) change your user agent to 'Mozilla/5.0', completing the illusion
that you are a browser.  Many sites don't like to see user agents like
"Python-urllib/2.4" :)

The rest is just understanding how forms work.  I've never used a
library, just POSTed/GETted the data directly to login and search
sites.  So I can't say how much easier it would be.  Probably quite a
bit.

You can POST data like:

urlopen("https://www.whatever.com/signin.dll",data=authPairs).close()

where authPairs is something like:

authPairs = urllib.urlencode({'userid':'andrew', 'password':'secret1234'})

You'll have to dig around in the html to see whether forms are using
POST or GET, and what variables they are passing, etc.  But that
should be enough to get you started.  Logged in, and able to fetch
pages and fill out forms manually.

Good luck,

Andrew

On 10/25/05, Ed Hotchkiss <[EMAIL PROTECTED]> wrote:
> looking to write a myspace wrapper/module. what is the best way (hopefully
> using the stdlib not an outside module) to connect to a website and (if
> possible, otherwise ill have to code it?) access forms with GET POST blah
> blah blah ...
>
> thanks!
>
> --
> edward hotchkiss
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor