[Tutor] exercise with classes 2nd attempt

2012-02-11 Thread Tonu Mikk
I am learning Python using the "Learn Python the Hard Way" book by Zed
Shaw.
 I reached exercise 42 where we learn about Python
classes.
 The exercise shows a game with one class that includes all the definitions
for playing the game.  For extra credit we are asked to create another
version of this game where we use two classes - one for the room
definitions and the other for the playing the game.

May attempt at creating two classes is here http://codepad.org/963vUgSt .
 I get the following error which I have been un-able to resolve.  Any
suggestions are welcome.

Traceback (most recent call last):
  File "ex42_3.py", line 233, in 
run()
  File "ex42_3.py", line 231, in run
room1.doIt()  # plays the game
  File "ex42_3.py", line 32, in doIt
self.obj.play() # use object
  File "ex42_3.py", line 20, in play
room = getattr(self, next)
AttributeError: 'Engine' object has no attribute 'central_corridor'

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


Re: [Tutor] exercise with classes

2012-02-09 Thread Tonu Mikk
On Mon, Feb 6, 2012 at 12:58 PM, Dave Angel  wrote:

> On 02/06/2012 01:24 PM, Tonu Mikk wrote:
>
>> Now I get an error:  NameError: global name 'self' is not define.
>>
>> Tonu
>>
>>
>>  Put your remarks after the stuff you quote.  You're top-posting, which
> makes the reply difficult to follow.
>
> Use copy/paste to describe an error message.  You retyped the one above,
> and added a typo.  Include the whole error, which includes the stack trace.
>
> If you had followed Nate's advice, you couldn't have gotten that error at
> all, so you'll also need to post the code that actually triggers the error.


Let's try this one more time.  I thought that I would try to make Alan's
code work.  Here is the version what I have now:

class Printer:
  def __init__(self,number=0):
 self.value = number
  def sayIt(self):
 print self.value

class MyApp:
  def __init__(self, aPrinter = None):
  if aPrinter == None: # if no object passed create one
 aPrinter = Printer()
  self.obj = aPrinter  # assign object
  def doIt():
  self.obj.sayIt() # use object

def test():
  p = Printer(42)
  a1 = MyApp()
  a2 = MyApp(p)   # pass p object into a2
  a1.doIt(self)   # prints default value = 0
  a2.doIt(self)   # prints 42, the value of p

test()

When I run it, I get an error:
Traceback (most recent call last):
  File "alan_class.py", line 22, in 
test()
  File "alan_class.py", line 19, in test
a1.doIt(self)   # prints default value = 0
NameError: global name 'self' is not defined



>
>
>
>
>
>
> --
>
> DaveA
>
>


-- 
Tonu Mikk
Disability Services, Office for Equity and Diversity
612 625-3307
tm...@umn.edu
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes

2012-02-06 Thread Tonu Mikk
Now I get an error:  NameError: global name 'self' is not define.

Tonu

On Mon, Feb 6, 2012 at 10:19 AM, Nate Lastname  wrote:

> Hey Tonu,
>
> The problem is that in your statement definition, you are not
> including the self argument.  Your definition needs to be something
> like:
> def dolt(self):
>   # Do stuff.
> For more info on the self keyword, see
> http://docs.python.org/tutorial/classes.html, section 9.3.2.
>
> On 2/6/12, Tonu Mikk  wrote:
> > Alan, thanks for explaining about passing objects to classes.  This is an
> > important concept for me to understand.
> >
> > I tried running the code, but run into an error that I could not resolve:
> >
> > TypeError: doIt() takes no arguments (1 given).
> >
> > Tonu
> >
> > On Thu, Feb 2, 2012 at 7:09 PM, Alan Gauld  >wrote:
> >
> >> On 02/02/12 17:36, Tonu Mikk wrote:
> >>
> >>  So far I have searched for info on how to pass variables from one class
> >>> to another and have been able to create a small two class program
> >>> (attached).   But I seem unable to generalize from here and apply this
> >>> to the game exercise.  What would you suggest for me to try next?
> >>>
> >>
> >> Remember that OOP is about creating objects from classes.
> >> You can pass an object to another rather than just the
> >> variables, in fact its preferable!
> >>
> >> Also remember that you can create many objects from one class.
> >> So just because you have one Room class doesn't mean you are
> >> stuck with one room object. You can have many and each can
> >> be connected to another.
> >>
> >> You can get rooms to describe themselves, you can enter a room.
> >> You might even be able to create new rooms or destroy existing ones.
> These
> >> actions can all be methods of your Room class.
> >>
> >> Here is an example somewhat like yours that passes objects:
> >>
> >> class Printer:
> >>   def __init__(self,number=0):
> >>  self.value = number
> >>   def sayIt(self):
> >>  print self.value
> >>
> >> class MyApp:
> >>   def __init__(self, aPrinter = None):
> >>   if aPrinter == None: # if no object passed create one
> >>  aPrinter = Printer()
> >>   self.obj = aPrinter  # assign object
> >>   def doIt()
> >>   self.obj.sayIt() # use object
> >>
> >> def test()
> >>   p = Printer(42)
> >>   a1  MyApp()
> >>   a2 = MyApp(p)   # pass p object into a2
> >>   a1.doIt()   # prints default value = 0
> >>   a2.doIt()   # prints 42, the value of p
> >>
> >> test()
> >>
> >> HTH,
> >>
> >> --
> >> Alan G
> >> Author of the Learn to Program web site
> >> http://www.alan-g.me.uk/
> >>
> >>
> >> __**_
> >> Tutor maillist  -  Tutor@python.org
> >> To unsubscribe or change subscription options:
> >> http://mail.python.org/**mailman/listinfo/tutor<
> http://mail.python.org/mailman/listinfo/tutor>
> >>
> >
> >
> >
> > --
> > Tonu Mikk
> > Disability Services, Office for Equity and Diversity
> > 612 625-3307
> > tm...@umn.edu
> >
>
>
> --
> My Blog - Defenestration Coding
>
> http://defenestrationcoding.wordpress.com/
>



-- 
Tonu Mikk
Disability Services, Office for Equity and Diversity
612 625-3307
tm...@umn.edu
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes

2012-02-06 Thread Tonu Mikk
Alan, thanks for explaining about passing objects to classes.  This is an
important concept for me to understand.

I tried running the code, but run into an error that I could not resolve:

TypeError: doIt() takes no arguments (1 given).

Tonu

On Thu, Feb 2, 2012 at 7:09 PM, Alan Gauld wrote:

> On 02/02/12 17:36, Tonu Mikk wrote:
>
>  So far I have searched for info on how to pass variables from one class
>> to another and have been able to create a small two class program
>> (attached).   But I seem unable to generalize from here and apply this
>> to the game exercise.  What would you suggest for me to try next?
>>
>
> Remember that OOP is about creating objects from classes.
> You can pass an object to another rather than just the
> variables, in fact its preferable!
>
> Also remember that you can create many objects from one class.
> So just because you have one Room class doesn't mean you are
> stuck with one room object. You can have many and each can
> be connected to another.
>
> You can get rooms to describe themselves, you can enter a room.
> You might even be able to create new rooms or destroy existing ones. These
> actions can all be methods of your Room class.
>
> Here is an example somewhat like yours that passes objects:
>
> class Printer:
>   def __init__(self,number=0):
>  self.value = number
>   def sayIt(self):
>  print self.value
>
> class MyApp:
>   def __init__(self, aPrinter = None):
>   if aPrinter == None: # if no object passed create one
>  aPrinter = Printer()
>   self.obj = aPrinter  # assign object
>   def doIt()
>   self.obj.sayIt() # use object
>
> def test()
>   p = Printer(42)
>   a1  MyApp()
>   a2 = MyApp(p)   # pass p object into a2
>   a1.doIt()   # prints default value = 0
>   a2.doIt()   # prints 42, the value of p
>
> test()
>
> HTH,
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>



-- 
Tonu Mikk
Disability Services, Office for Equity and Diversity
612 625-3307
tm...@umn.edu
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] exercise with classes

2012-02-02 Thread Tonu Mikk
I am learning Python using the "Learn Python the Hard Way" book by Zed
Shaw.
 I reached exercise 42 where we learn about Python
classes.
 The exercise shows a game with one class that includes all the definitions
for playing the game.  For extra credit we are asked to create another
version of this game where we use two classes - one for the room
definitions and the other for the playing the game.  I feel stumped and
don't know how to go about creating this game with two classes.

So far I have searched for info on how to pass variables from one class to
another and have been able to create a small two class program (attached).
  But I seem unable to generalize from here and apply this to the game
exercise.  What would you suggest for me to try next?

Thank you,

Tonu


classes_test.py
Description: Binary data
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using a for loop with feedparser

2009-01-27 Thread Tonu Mikk
Kent, you are absolutely correct.  I did not need the line number 5 at 
all.   This was my first  program that addressed a "real life" 
scenario.  I am excited that it worked.


Thank you,
Tonu

Kent Johnson wrote:

On Mon, Jan 26, 2009 at 4:53 PM, Tonu Mikk  wrote:
  

I now have a new question related to the same project.  I currently have a
code snippet like this:

1.for item in d.entries:
2.link = Link(url=item.link)
3.link.save()
4.user = User.objects.get(id=1)
5.link = Link.objects.get(id=1)
6.bookmark = Bookmark (
7.title = item.title,
8.desc = item.description,
9.link = link,
10.  user = user,
11.  )
12.  bookmark.save()

I need to increment the link id (line 5) for each item in the d.entries.  I
tried "link = Link.objects.get(id=id+1)" and "link =
Link.objects.get((id=1)+1)", but both of them generated errors.   I am not
quite sure where to go from here.



Are you trying to retrieve the Link you just saved in line 3? Why not
just use the link variable you already have?

Kent
  



--
Tonu Mikk
Educational Technology Consultant
Digital Media Center - dmc.umn.edu
tm...@umn.edu 612 625-9221

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


Re: [Tutor] using a for loop with feedparser

2009-01-26 Thread Tonu Mikk
I now have a new question related to the same project.  I currently have 
a code snippet like this:


1.for item in d.entries:
2.link = Link(url=item.link)
3.link.save()
4.user = User.objects.get(id=1)
5.link = Link.objects.get(id=1)
6.bookmark = Bookmark (
7.title = item.title,
8.desc = item.description,
9.link = link,
10.  user = user,
11.  )
12.  bookmark.save()

I need to increment the link id (line 5) for each item in the 
d.entries.  I tried "link = Link.objects.get(id=id+1)" and "link = 
Link.objects.get((id=1)+1)", but both of them generated errors.   I am 
not quite sure where to go from here.


Thank you,
Tonu

Tonu Mikk wrote:

Tonu Mikk wrote:

Hello,

I am trying to parse a feed and insert some feed elements into a 
Django project database.   I believe I should be able to use for loop 
to iterate over the list elements.  I am using the Universal Feed 
parser (http://www.feedparser.org/).  I am able to extract items one 
by one and insert them into a database (code below).   As my first 
try at this, I would like to print the titles of all the feed 
entries.  I tried this


import feedparser
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')
for item in d.entries:
   print item

This prints all the feed entries and their values.  Any thoughts on 
how I could just print the titles?
I found out the answer after trying some more.  To print the title 
only, I need to do this:


for item in d.entries:
   print item.title



Code to save a single feed item into the database:
#!/usr/local/bin/python
import feedparser
import os.path
import sys, os
import django.contrib.auth
sys.path.append ('/home/dmc/projects/django_bookmarks')
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from django.contrib.auth.models import User
from bookmarks.models import *
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')

link1 = Link(url=d.entries[0].link)
link1.save()
user = User.objects.get(id=1)
link = Link.objects.get(id=1)
bookmark = Bookmark (
   title = d.entries[0].title,
   desc = d.entries[0].description,
   link = link,
   user = user,
)
bookmark.save()
___
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] using a for loop with feedparser

2009-01-26 Thread Tonu Mikk

Tonu Mikk wrote:

Hello,

I am trying to parse a feed and insert some feed elements into a 
Django project database.   I believe I should be able to use for loop 
to iterate over the list elements.  I am using the Universal Feed 
parser (http://www.feedparser.org/).  I am able to extract items one 
by one and insert them into a database (code below).   As my first try 
at this, I would like to print the titles of all the feed entries.  I 
tried this


import feedparser
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')
for item in d.entries:
   print item

This prints all the feed entries and their values.  Any thoughts on 
how I could just print the titles?
I found out the answer after trying some more.  To print the title only, 
I need to do this:


for item in d.entries:
   print item.title



Code to save a single feed item into the database:
#!/usr/local/bin/python
import feedparser
import os.path
import sys, os
import django.contrib.auth
sys.path.append ('/home/dmc/projects/django_bookmarks')
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from django.contrib.auth.models import User
from bookmarks.models import *
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')

link1 = Link(url=d.entries[0].link)
link1.save()
user = User.objects.get(id=1)
link = Link.objects.get(id=1)
bookmark = Bookmark (
   title = d.entries[0].title,
   desc = d.entries[0].description,
   link = link,
   user = user,
)
bookmark.save()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



--
Tonu Mikk
Educational Technology Consultant
Digital Media Center - dmc.umn.edu
tm...@umn.edu 612 625-9221

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


[Tutor] using a for loop with feedparser

2009-01-26 Thread Tonu Mikk

Hello,

I am trying to parse a feed and insert some feed elements into a Django 
project database.   I believe I should be able to use for loop to 
iterate over the list elements.  I am using the Universal Feed parser 
(http://www.feedparser.org/).  I am able to extract items one by one and 
insert them into a database (code below).   As my first try at this, I 
would like to print the titles of all the feed entries.  I tried this


import feedparser
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')
for item in d.entries:
   print item

This prints all the feed entries and their values.  Any thoughts on how 
I could just print the titles? 


Thank you,
Tonu


Code to save a single feed item into the database:
#!/usr/local/bin/python
import feedparser
import os.path
import sys, os
import django.contrib.auth
sys.path.append ('/home/dmc/projects/django_bookmarks')
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from django.contrib.auth.models import User
from bookmarks.models import *
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')

link1 = Link(url=d.entries[0].link)
link1.save()
user = User.objects.get(id=1)
link = Link.objects.get(id=1)
bookmark = Bookmark (
   title = d.entries[0].title,
   desc = d.entries[0].description,
   link = link,
   user = user,
)
bookmark.save()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Livewires

2007-09-10 Thread Tonu Mikk
Hi Sacha,  I am very much a beginner to Python myself, but I tried my 
hand on the Livewires modules.  Here is how I solved the challenge. 

from livewires import *
begin_graphics()
allow_moveables()
x=0
y=0
c = circle(x, y,5)
while x < 640:
x=x+5
y=y+3.822
move_to (c, x, y)
time.sleep(0.1)

Tonu
sacha rook wrote:
> Hi all
>  
> just learning python really and been using the livewires tutorial / 
> worksheets to get some experience.
>  
> I have hit an issue which is just my lack of understanding around 
> looping concepts and execution.
>  
> My issue:
>  
> in worksheet 5-robots.pdf attached, page 4 the challenge
>  
> "Challenge: Write a loop that makes the circle move smoothly from 
> (0,0) to (640,480): in other words, from the bottom left
> to the top right of the screen."
>  
> this has got me a bit stumped because its an (x,y) co-ordinate pair 
> that I want to update.
> I think in a loop i need to draw a circle, move a circle, remove the 
> circle.
>  
> I thought I needed to for loops to iterate through two ranges but this 
> is wrong, here is my code though!
>  
> from livewires import *
> begin_graphics()
>  
> allow_moveables()
> x=range(10,640,10)
> y=range(10,480,10)
> for xco in x:
> for yco in y:
> c = circle(xco,yco,5)
> move_to(c, xco,yco)
> #remove_from_screen(c) /*commented this out to see output on 
> graphics window */
> end_graphics()
>  
> Can anyone look at the worksheet challenge and my poor code and show 
> me the error of my ways? :)
> I appreciate it may be my inexperience in program flow/logic which is 
> the problem also, I don't my help or suggestion to improve in any area.
> Thanks for your help in advance
> Sacha
>  
>  
>
> 
> Play Movie Mash-up and win BIG prizes! 
> 
>
> ___
> 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] Livewires - stuck on a class

2007-08-15 Thread Tonu Mikk


Alan Gauld wrote:
> "Tonu Mikk" <[EMAIL PROTECTED]> wrote
>
>   
>> I create more robots in this way which seems to work:
>> class Robot:
>>pass
>> 
>
> By using an empty class you are losing m,uch of the power of classes.
>
>   
I would need to learn more to handle the classes better.  In this case, 
I am following the Livewires tutorial pretty closely which uses an empty 
class.
>> def move_robot():
>>for x in robots:
>>while 1:
>>if robot.x + 0.5< player.x and robot.y +0.5< player.y:
>> 
>
> You are doing *for x in robots* but then moving *robot* not x.
>   
Yes, that was my mistake.  I changed the line to "for robot in robots:" 
and now it works!
>
> Also it would be better IMHO to use if/elif rather than all those 
> if/breaks.
>   
I followed your advice here.  It does make the code a bit more clear.

Thanks a lot,

Tonu

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


Re: [Tutor] Livewires - stuck on a class

2007-08-14 Thread Tonu Mikk
[EMAIL PROTECTED] wrote:
> >Then I was hoping to repeat the sequence for moving the robots placed
> >in 
> >the robots list by using this code:
> >for x in robots:
> ># code for moving the robots
>
> Glancing at your code to move the robots.  I don't see you using you x 
> from for x in robots.  Since in your placement code robot is assigned 
> to a new robot each time through the loop, the placement works.  In 
> your movement you don't change what robot is representing.
>
> I think you want to change you line:
> for x in robots:
> to become...
> for robot in robots:
>
>
Thank you!  This did the trick. 

Tonu

-- 
Tonu Mikk
Educational Technology Consultant
Digital Media Center - dmc.umn.edu
[EMAIL PROTECTED] 612 625-9221

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


Re: [Tutor] Livewires questions

2007-07-29 Thread Tonu Mikk
Luke Paireepinart wrote:
>>
>>
>> Sure, sure.  I'm glad it at least ran.  I don't have livewires 
>> installed so I wasn't able to test any of the code I wrote.
>>
>> Thanks again for your help.  If you have suggestions on the 't' key,
>> please share them.  This seems to be the one issue preventing me 
>> from
>> going forward.
>>
>> Tonu
>>
>>
> Actually, I may not have time for this after all.  I'm in the final 
> throes of a Differential Equations class (it's really accelerated to 
> fit all in one month) and even on weekends I have to study for it.  
> Luckily it's done in 9 class days (we do class 12-1:40 M-F, so that 
> puts the Final on Thursday) so I will gladly offer any help you need 
> after that time.
> But until then, I'm not sure when I'll be able to do anything.
> P.S. I forgot to hit reply-all on the previous e-mail, which is why I 
> left it intact.
Luke,  Good luck with the class.  It seems a challenging one judging by 
the name :-).  I was able to get the player to be moved into a different 
location on the grid when the "t" key is pressed.  The answer was, as it 
often is, in reading the manual :-), in this case the Graphics worksheet 
that talks about removing objects which is a bit of the Livewires 
magic.  Here is my code that removed the player from the board and 
placed it in a new location when the 't' keys is pressed:

if 't' in keys:
remove_from_screen (player_shape)
place_player()
break

I will now venture forth with the rest of the instructions in the 
exercise.  Thank you all for  helping me over the hump!

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


Re: [Tutor] Livewires questions

2007-07-27 Thread Tonu Mikk
Luke Paireepinart wrote:
>> def place_robot():
>> global robot_x
>> global robot_y
>> global robot_shape
>> robot_y = random_between(0,47)-0.5
>> robot_x = random_between(0,63)-0.5
>>   
> I'm not too clear why you're subtracting 0.5 here.
> Doesn't this make the robot's center on the grid lines, rather than 
> having the robot occupy a full square on the grid?
> I guess that's the intended behavior, huh?
This is particular to Livewires module I believe.  I am dealing with the 
circle (player) whose position is defined by the center of the circle 
and a square (robot) whose position is defined by the first two 
coordinates of the square.  I subtract 0.5 here so that when the random 
numbers picked for both the circle and the the square are the same, then 
visually the square is right on top of the circle. 
>
>> robot_shape = box(10*robot_x, 
>> 10*robot_y,10*robot_x+10,10*robot_y+10)
>> def move_player():
>> while 1:
>> global player_x
>> global player_y
>>   if 't' in keys:
>> place_player()
>> break
>>   
> You can't reuse your place_player function here, because you put the 
> initialization (the creation) of the graphics that represent the 
> player _inside_ of the place_player function.  Well, technically you 
> could, but it would be better instead to use move_to(player_shape, x, 
> y) because this doesn't overwrite the old player_shape circle.
> Also, will this work if the player presses "T"?
Ok, I made some progress on the pressing of the "T" key, but it is still 
not entirely correct.  The reason why pressing the 't' key was not 
working initially was because I had omitted the 't' key in the main 
while loop.  As a result, the code never executed the move_player code 
when I pressed the 't' key.  Now if I execute the code above, I get a 
new player circle placed on the grid, but the old player stays there as 
well.  I somewhat expected this because I am not getting rid of the old 
player by calling place_player() function.  How could I eliminate the 
old player circle from the grid?  When I use the move_to(player_shape, 
player_x*10, player_y*10), then the player just stays in the same 
place.  This too I expect, since by using move_to, I am not calling new 
player_x and player_y coordinates.
>> if '8' in keys:
>> move_to(player_shape,player_x*10 ,player_y*10 + 10)
>> player_y = player_y + 1
>> if player_y > 47:
>> player_y = player_y -1
>> else:
>> pass
>>   
> 'if' statements don't have to have an 'else' clause.
> else:
>pass
> is just a waste of 2 lines.  'pass' does nothing, and since the 'else' 
> is not required, you may as well leave it off.
I thought so too.  Part of the problem I had with testing my code was 
that I was using the PythonWin editor that comes with ActivePython.  
Often the PythonWin editor would crash and I wasn't always sure if it 
was because of my code, or because of the editor itself.  It somehow 
seemed that when I had the else: pass in the code, the editor behaved 
better. I have since then started to use IDLE which works much better.  
Thank you for pointing this out BTW.  It takes some of the guess work 
out :-).
>>
>>  
> This big block of movement code can be shortened into a few lines.
> I am not sure how much python you know, so if any of this doesn't make 
> sense, let me know.
I am just a newbie in Python.  I like the way you were able to think 
about the code to make the code much more succinct.  I will need to 
learn a bit more before I understand it exactly.  For now, I will try to 
stick with my long, ugly, inefficient code, that I have puzzled together 
and understand.  Your example has not gone wasted however, it will be 
used later when I am more familiar with Python and try to improve my code. 
>>  
> Also, I just realized that you're using a while loop inside of your 
> move_player and move_robots function.
> But you only call these functions if you KNOW that a movement key was 
> pressed.
> Basically, this means that one of your 'if' conditions will ALWAYS be 
> true inside of your while: loops, and you'll break out of it.
>
> The only reason why you might want this is that it would immediately 
> exit as soon as it finds the first key that was pressed, but the
> same thing could be performed using an if-elif chain.  But I doubt you 
> want this to happen.
I will experiment with the if statements there.  Is it better to use 
if-elif chain over the while loop when both can work?
>
> Also there are other problems - eg. your keys is assigned to the keys 
> that were currently pressed, so if during the time.sleep(0.5) I might
> press and then release a key, and it would be lost in your code.
Yes, I see this when I run the code.  Unfortunately, I don't know how 
else to do it since it is the only way that they show in the Livewires 
exercise.  I am OK it being imperfect for now.
> The code 

Re: [Tutor] Livewires questions

2007-07-26 Thread Tonu Mikk
Eric Brunson wrote:
> Tiger12506 wrote:
>   
>>> Based on your guidance, I figured it out.  I need to use a return 
>>> statement, which I had not encountered before.  Now I wrote my 
>>> definitions in this way:
>>>
>>> def collided():
>>>if player_x == robot_x+0.5 and player_y == robot_y+0.5:
>>>   return True
>>> 
>>>   
>
> Granting that I have not looked at any of the Livewires modules, I just 
> wanted to say...
>
> A general check for collision would probably involve the distance 
> formula from geometry
>
> collided( (x1,y1), (x2,y2) ):
>return( sqrt( (x1-x2)**2 + (y1-y2)**2 ) < 1 )
>
> but could probably be simplified to something like:
>
> def collided( (x1,y1), (x2,y2) ):
>return( abs( x1 - x2 ) < .5 and abs( y1 - y2 ) < .5 )
>
>   
Thanks Eric for your suggestions.  I believe Livewires modules have 
simplified collision checking for programming novices like myself.  
There are two shapes that I am working with, a circle and a square.  The 
position of the circle is defined by the center coordinates whereas the 
position of the square is defined by the lower left corner of the 
square.  When my circle is 0.5 points in diameter, I can add this much 
to both x and y coordinates of the square which will then give me the 
point where the square is sitting on top of the circle.  It took me a 
long time to figure this out.  I had to re-read the Graphics guide sheet 
that came with Livewires multiple times to try to get it to work 
correctly.  I believe this part of my code is OK. 

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


Re: [Tutor] Livewires questions

2007-07-26 Thread Tonu Mikk
Tiger12506 wrote:
>> Based on your guidance, I figured it out.  I need to use a return 
>> statement, which I had not encountered before.  Now I wrote my 
>> definitions in this way:
>>
>> def collided():
>>if player_x == robot_x+0.5 and player_y == robot_y+0.5:
>>   return True
>> 
>  
> This could be simplified more.
> Here's an example as a hint. These two functions are the same.
>
> def f():
>   if a == b and c == d:
> return True
>
> def g():
>   return (a==b and c == d)
>
>   
I got it.  I will do it like this:
def collided():
    return (player_x == robot_x+0.5 and player_y == robot_y+0.5)

Thank you,
Tonu

-- 
Tonu Mikk
Educational Technology Consultant
Digital Media Center - dmc.umn.edu
[EMAIL PROTECTED] 612 625-9221

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


Re: [Tutor] Livewires questions

2007-07-26 Thread Tonu Mikk
Luke, thank you for your quick and complete response.  Based on your 
suggestions I have already made some progress!  BTW, I am so glad that I 
can ask this list my Python questions and get help.  I began feeling 
quite stuck and not knowing where to turn for help.  So, thank you for 
the great service!

Luke Paireepinart wrote:
> Tonu Mikk wrote:
>> Thanks for offering to help!  I am following the Livewires exercise 
>> (attached file "5-robots.pdf").  I have gotten as far as page 7.  
>> Attached is also my code so far in robotsarecoming-teleport.py.
>> Question 1.  I was checking for collision of a robot and player first 
>> in this way:
>>
>> def check_collisions():
>>if player_x == robot_x+0.5 and player_y == robot_y+0.5:
>>   print 'You have been caught'
>>
>> This was working fine.  I then tried to create a definition like this:
>>
>> def collided():
>>player_x == robot_x+0.5 and player_y == robot_y+0.5
> I haven't looked at your code yet, but this doesn't seem like a very 
> good way to check for collisions,
> unless the player moves on a grid of 0.5 at a time, and you guarantee 
> that you can check if the player collided with a robot on every move.
> even so, doesn't this only collide with the robot if the player hits 
> the bottom-right corner?
Yes, this indeed looks strange.  I believe it is particular to this 
Livewires exercise.  The reason it is strange is that I am comparing a 
position of two different shapes, a circle and a square.  The position 
for the circle is in the center of the circle and it is defined by 
player_x and player_y coordinates.  The position of the square is 
defined by the first two coordinates that make up a square robot_x and 
robot_y.  The circle radius is 0.5.  It took me a lng time to figure 
out how to write my code so that when the robot sits exactly on top of 
the player, there is a message "you have been caught" :-) .  When I run 
the code, the robot will sit on top of the player and I can have the  
message printed - yeaaah!
>
>>
>> and then check for collisions in this way (as in my code):
>> def check_collisions():
>>if collided() == 1:
>>print 'You have been caught'
> The reason this isn't working is because your function 'collided' 
> doesn't return anything.
> Consider this example:
>
> def foo():
>"Hello"
>
> What do you expect to happen when you call foo()?
> 1) "Hello" won't be printed, because there is no 'print' statement here.
> 2) "Hello" won't be returned, because you have no return statement.
> So what does happen, then?
> well, foo() creates a string in memory with "Hello" stored in it, but 
> there are no variables referenced to it, so nothing happens.
> Basically, the only thing foo() accomplishes is that it wastes memory 
> until "Hello" is garbage collected and deleted.
>
> Now consider this:
> def foo():
>a == b and b == c
>
> What do you expect to happen here?
> It's similar to the above example.
> a == b is evaluated.
> if it's true, b == c is evaluated.
> if it's true, then the value True is there, but it's not assigned to 
> any variables, so it just disappears.
> Can you see now why your code doesn't work?
>
> Here's an example of a function you'd want to look at to give you an 
> idea of what to do:
>
> def foo():
>  return a < b
>> But this isn't printing out anything when the player and robot 
>> collide.  I think I need to pass a variable of collided somehow, but 
>> I am not sure how.  I also tried following:
>> def check_collisions():
>>if collided()
>>   print 'You have been caught'
>> but this isn't working either.
> This is because collided() is not returning anything.
> Try this:
> print collided()
> you will get this output:
> None
Based on your guidance, I figured it out.  I need to use a return 
statement, which I had not encountered before.  Now I wrote my 
definitions in this way:

def collided():
if player_x == robot_x+0.5 and player_y == robot_y+0.5:
   return True

Then I use that value in another definition like this:

def check_collisions():
if collided() == 1:
   print "You have been caught"

Which is displaying the message when the robot sits on top of the player.
   
>>
>> Question 2.  I created a if statement to check if the "t" key is 
>> pressed on a keyboard.  If it is, I want the player to be placed on 
>> another location on the grid.  However nothing happens when I press 
>> the "t" key.  I am 

[Tutor] Livewires Python course

2007-07-25 Thread Tonu Mikk
Hello, I am at a very beginning on trying to learn Python.  So far I 
have read first few chapters of Alan Gauld tutorials, and completed all 
the exercises of Guido van Robot (http://gvr.sourceforge.net/).  I also 
began reading and coding the Livewires course exercises 
(http://www.livewires.org.uk/python/).  I have gotten through the first 
4 exercise, but got stuck with the last one where we build a robot 
game.  The Livewires coding exercise uses modules that can be downloaded 
from their website.  Would anyone be willing to install Livewires 
modules on their computer and assist me with the coding?  I have 
specific questions, but the code could be difficult to read because it 
takes advantage of the imported modules.

Thank you,
Tonu
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to run a text file in an interpreter?

2007-03-30 Thread Tonu Mikk
I started reading Instant Python tutorial ( 
http://hetland.org/writing/instant-python.html ) and came across this 
section:
"/Note:/ To get the examples working properly, write the programs in a 
text file and then run that with the interpreter; do /not/ try to run 
them directly in the interactive interpreter - not all of them will work. "

I do not know what the author means by running a text file with the 
interpreter. I noticed that I came across an obstacle early on in trying 
out the code.

I tried writing

|first, second = second, first

into the PythonWin interpreter, but this resulted in the following error after 
pressing the return key:

Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'second' is not defined

I imagine that I need to create a text file first and then run it with the 
interpreter.  How do I do that? 

Thank you,
Tonu 
|


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


[Tutor] how to run a text file in an interpreter?

2007-03-30 Thread Tonu Mikk
Hello, I am new to the list and to programming in  general.   I want to 
learn Python as I find it an interesting language.  I also support an 
application that is written in Python called Roundup 
http://roundup.sf.net .  I find that I would like to make small tweaks 
to the program that require a knowledge of Python.

Now to my question.  I started reading Instant Python tutorial ( 
http://hetland.org/writing/instant-python.html ) and came across this 
section:
"/Note:/ To get the examples working properly, write the programs in a 
text file and then run that with the interpreter; do /not/ try to run 
them directly in the interactive interpreter - not all of them will work. "

I do not know what the author means by running a text file with the 
interpreter. I noticed that I came across an obstacle early on in trying 
out the code.

I tried writing

|first, second = second, first

into the PythonWin interpreter, but this resulted in the following error after 
pressing the return key:

Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'second' is not defined

I imagine that I need to create a text file first and then run it with the 
interpreter.  How do I do that? 

Thank you,
Tonu 
|

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