Re: [Tutor] Trouble with exercise regarding classes
Ok I think I got it. Thanks everybody. And sorry for the late reply. My classes have just started so learned python unfortunately must be bumped down on the priority list On Thu, Aug 26, 2010 at 4:32 AM, Alan Gauld wrote: > > "Andrew Martin" wrote > > I want to compare zenith, a floating point number, with the current y >> value? >> I thought the current y value could be retrieved by Projectile.getY. >> > > Projectile.getY is a reference to the getY method of the Projectile class. > (See the separate thread on function objects for more on this topic) > > You want to execute the method so you need parens on the end. > > But, you also want to execute it for the cball instance. > You have already done this earlier in your code, here: > >while cball.getY() >= 0: >>>>> >>>> > So you just need to make the if test compatible with that: > > > if Projectile.getY > zenith: >>> >> > becomes > > if cball.getY() > zenith: > > And similarly for the assignment > >zenith = Projectile.getY() >>>>> >>>> becomes > zenith = cball.getY() > > > As an aside you can do this using Projectile, but its bad practice: > > Projectile.getY(cball) > > This explicitly provides the self argument instead of Python doing > it for you when you use the instance. We can use this technique when > calling inherited methods inside a class method definition. Anywhere > else its best to use the instance to call a method. > > HTH, > > > -- > Alan Gauld > 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 > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with exercise regarding classes
All I want to do is add a line that displays that maximum height the cannonball reaches. I created a variable zenith to store the highest y value. I then wanted to compare the current y value of the cannonball to zenith while the cannonballs y value is greater than zero. If the cannonballs current y value is greater than zenith, I want to have the current value replace zenith. Finally, once the cannonball has reaches y = 0, I wanted the program to write out the value for zenith. I want to compare zenith, a floating point number, with the current y value? I thought the current y value could be retrieved by Projectile.getY. And how do I call the getY using the instance? On Wed, Aug 25, 2010 at 7:24 PM, Alan Gauld wrote: > > "Andrew Martin" wrote > > > However, when I did so I got this error: "TypeError: unbound method getY() >> must be called with Projectile instance as first argument (got nothing >> instead) " >> > > def main(): >>>angle, vel, h0, time = getInputs() >>>cball = Projectile(angle, vel, h0) >>> >> > cball is a Projectile instance > > > zenith = 0.0 >>>while cball.getY() >= 0: >>> >> > So this is fine > > > cball.update(time) >>> >> > > if Projectile.getY > zenith: >>>zenith = Projectile.getY() >>> >> > But what are you doing here? > You are trying to compare the getY method of the class with a floating > point number? > Then you call getY using the class rather than the instance? > I'm confused - and so is Python... > > > -- > Alan Gauld > 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 > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trouble with exercise regarding classes
I just starting programming and am trying to learn some python (ver 2.6). I am reading Python Programming: An Introduction to Computer Science by John Zelle. In chapter ten, the first programming exercise asks the reader to modify code from the chapter (below) . The code I added is highlighted. However, when I did so I got this error: "TypeError: unbound method getY() must be called with Projectile instance as first argument (got nothing instead) " Can someone help me out with what I am doing wrong? Please be as straitforward as you can. I am still struggling with classes Thanks a lot # cball4.py > # Simulation of the flight of a cannon ball (or other projectile) > # This version uses a separate projectile module file > > from projectile import Projectile > > def getInputs(): > a = input("Enter the launch angle (in degrees): ") > v = input("Enter the initial velocity (in meters/sec): ") > h = input("Enter the initial height (in meters): ") > t = input("Enter the time interval between position calculations: ") > return a,v,h,t > > def main(): > angle, vel, h0, time = getInputs() > cball = Projectile(angle, vel, h0) > zenith = 0.0 > while cball.getY() >= 0: > cball.update(time) > if Projectile.getY > zenith: > zenith = Projectile.getY() > print "\nDistance traveled: %0.1f meters." % (cball.getX()) > print "The heighest the cannon ball reached was %0.1f meters." % > (zenith) > > if __name__ == "__main__": main() > > > # projectile.py > > """projectile.py > Provides a simple class for modeling the flight of projectiles.""" > > from math import pi, sin, cos > > class Projectile: > > """Simulates the flight of simple projectiles near the earth's > surface, ignoring wind resistance. Tracking is done in two > dimensions, height (y) and distance (x).""" > > def __init__(self, angle, velocity, height): > """Create a projectile with given launch angle, initial > velocity and height.""" > self.xpos = 0.0 > self.ypos = height > theta = pi * angle / 180.0 > self.xvel = velocity * cos(theta) > self.yvel = velocity * sin(theta) > > def update(self, time): > """Update the state of this projectile to move it time seconds > farther into its flight""" > self.xpos = self.xpos + time * self.xvel > yvel1 = self.yvel - 9.8 * time > self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0 > self.yvel = yvel1 > > def getY(self): > "Returns the y position (height) of this projectile." > return self.ypos > > def getX(self): > "Returns the x position (distance) of this projectile." > return self.xpos > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difficulty Understanding Example Code for Blender Script
Oh ok. So orientation is an optional parameter. That makes sense. Alright well thanks for the help And yeah next time it would probably be better to try the blender forums. thanks though On Thu, Jul 22, 2010 at 3:06 AM, David Hutto wrote: > On Thu, Jul 22, 2010 at 2:47 AM, Marc Tompkins > wrote: > > On Wed, Jul 21, 2010 at 9:48 PM, Andrew Martin > > wrote: > >> > >> This code was part of a Blender script to build a 3d bar graph, so I > don't > >> know if understanding Blender is a prereq for understanding this code. > The > >> function is for the axis labels. > >> > >> def label(text,position,orientation='z'): > >> txt=Text3d.New('label') > >> txt.setText(text) > >> ob=Scene.GetCurrent().objects.new(txt) > >> ob.setLocation(*position) > >> if orientation=='x': > >> ob.setEuler(-pi/2.0,0,0) > >> elif orientation=='z': > >> ob.setEuler(0,0,pi/2.0) > >> print 'label %s at %s along %s' %(text,position,orientation) > >> > >> I understand it for the most part except for the orientation part. I > >> assume orientation is for the 3d text object, but how is it determined > >> whether it is x or z? > > > > I don't use Blender myself, so this will be a more generic, high-level > > answer... > >> > >> def label(text,position,orientation='z'): > > > > This definition specifies that label() takes two mandatory parameters - > text > > and position - and one optional parameter, orientation. What makes > > "orientation" optional is the fact that a default value is supplied: > > "orientation='z'". In other words, "orientation" is equal to "z" unless > you > > specify otherwise in your call to label(). > > Seeing as how blender is 3d graphics, have you tried the 'newbie > fidget with it', and typed in w(quaternion),x, or y to see what > occurs. Also, have you looked into the hierarchy to see if z, which > looks as though it's contained in a string, is an input variable > declared elsewhere as an integer, or represents something else in it's > usage. Z can mean global, or object orientation in blender from what I > see. > > > > > Take a look at this section of the Python docs: > > > http://docs.python.org/tutorial/controlflow.html#more-on-defining-functions > > > > Hope that helps... > > > > > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difficulty Understanding Example Code for Blender Script
This code was part of a Blender script to build a 3d bar graph, so I don't know if understanding Blender is a prereq for understanding this code. The function is for the axis labels. def label(text,position,orientation='z'): txt=Text3d.New('label') txt.setText(text) ob=Scene.GetCurrent().objects.new(txt) ob.setLocation(*position) if orientation=='x': ob.setEuler(-pi/2.0,0,0) elif orientation=='z': ob.setEuler(0,0,pi/2.0) print 'label %s at %s along %s' %(text,position,orientation) I understand it for the most part except for the orientation part. I assume orientation is for the 3d text object, but how is it determined whether it is x or z? Please keep replies simple; I am a newcomer to programming (and if this makes no sense at all, I would appreciate polite inquiries for more info). Using Blender 2.49b and Python 2.6 Thanks a bunch, amartin7211 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Return error message for my script in Blender
Yeah ok I get it. I have to return something. I looked at the sample code provided on the book's website and found out what I am supposed to return. Thanks. I appreciate the responses, especially to this bonehead question. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Return error message for my script in Blender
I am new to Blender and Python (2.6 on vista) and was trying to follow a tutorial in the book Blender 2.49 Scripting by Michel Anders. I was trying to write a script that would create a user interface where the user could select various aspect of an insect and the code would create a polygonal bug. However, when I copied code exactly from the book, I got an error saying "'return' outside function". Here is the code I had in the text editor: #!BPY import Blender import mymesh2 Draw = Blender.Draw THORAXSEGMENTS = Draw.Create(3) TAILSEGMENTS = Draw.Create(5) LEGSEGMENTS = Draw.Create(2) WINGSEGMENTS = Draw.Create(2) EYESIZE = Draw.Create(1.0) TAILTAPER = Draw.Create(0.9) if not Draw.PupBlock('Add CreepyCrawly', [\ ('Thorax segments:' , THORAXSEGMENTS, 2, 50, 'Number of thorax segments'), ('Tail segments:' , TAILSEGMENTS, 0, 50, 'Number of tail segments'), ('Leg segments:' , LEGSEGMENTS, 2, 10, 'Number of thorax segments with legs'), ('Wing segments:' , WINGSEGMENTS, 0, 10, 'Number of thorax segments with wings'), ('Eye size:' , EYESIZE, 0.1,10, 'Size of the eyes'), ('Tail taper:' , TAILTAPER, 0.1,10, 'Taper fraction of each tail segment'),]): return Anybody know why I keep getting this error? Thanks a lot amartin7211 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems installing
How exactly can I go about deleting __init__.pyc? Sorry, I am new to this so I need everything spelled out for me. On Wed, Jun 30, 2010 at 2:30 AM, Marc Tompkins wrote: > On Tue, Jun 29, 2010 at 10:35 PM, Andrew Martin wrote: > >> I just downloaded Python 2.6.5 onto my windows vista laptop. I am >> attempting to install "escript version 3: Solution of Partial Differential >> Equations (PDE) using Finite Elements (FEM)." I downloaded the files and >> manually placed them in their appropriately place on my computer according >> to the ReadMe file. However, when I try to import escript, I get an error: >> >> >>> import esys.escript >> >> Traceback (most recent call last): >> File "", line 1, in >> import esys.escript >> ImportError: Bad magic number in >> C:\Python26\lib\site-packages\esys\escript\__init__.pyc >> >>> >> >> Can anybody lend me a hand? >> >> I don't know anything about escript, I'm afraid, but try this - .pyc > files are always replaceable, as long as the .py or .pyw files they > correspond to still exist. So delete __init__.pyc (I'm assuming that > there's also a __init__.py file) and try again. > > Of course, this is only a solution if the problem is a corrupt .pyc file, > which I've run into on a couple of occasions. If it's something else... > > Hope that helps. > > -- > www.fsrtechnologies.com > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems installing
I just downloaded Python 2.6.5 onto my windows vista laptop. I am attempting to install "escript version 3: Solution of Partial Differential Equations (PDE) using Finite Elements (FEM)." I downloaded the files and manually placed them in their appropriately place on my computer according to the ReadMe file. However, when I try to import escript, I get an error: >>> import esys.escript Traceback (most recent call last): File "", line 1, in import esys.escript ImportError: Bad magic number in C:\Python26\lib\site-packages\esys\escript\__init__.pyc >>> Can anybody lend me a hand? Thanks, amartin7211 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pydoc?
On Fri, Jun 18, 2010 at 7:13 PM, Andrew Martin wrote: > Alright I got it. Although i didn't end up doing any typing. All I did was > go to start/module docs and then press open browser. > > Thanks again and next time i will supply more info with the question ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pydoc?
Alright I got it. Although i didn't end up doing any typing. All I did was go to start/module docs and then press open browser. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] pydoc?
Hey, everyone, I am new to programming and just downloaded Python 2.6 onto my windows vista laptop. I am attempting to follow 4.11 of the tutorial called "How to Think Like a Computer Scientist: Learning with Python v2nd Edition documentation" ( http://openbookproject.net/thinkcs/python/english2e/ch04.html). However, I am having some trouble. I am trying to use pydoc to search through the python libraries installed on my computer but keep getting an error involving the $ symbol. $ pydoc -g SyntaxError: invalid syntax Can anyone help me out? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems with Importing into the Python Shell
Hey, everyone, I am new to programming and just downloaded Python 2.6 onto my windows vista laptop. I am attempting to follow 4.11 of the tutorial called "How to Think Like a Computer Scientist: Learning with Python v2nd Edition documentation" ( http://openbookproject.net/thinkcs/python/english2e/ch04.html). However, I am having some trouble. First off, I do not understand how to import things into the python shell. I have a script I saved as chap03.py, but when I try to import it into the python shell i get an error like this: >>> from chap03 import * Traceback (most recent call last): File "", line 1, in from chap03 import * File "C:/Python26\chap03.py", line 2 print param param ^ SyntaxError: invalid syntax >>> The chap03.py file is a simple one that looks like this: def print_twice(param): print param param My second problem is that I need to install and import GASP in order to follow the tutorial. When I tried to do import it, I ran into an error like this: >>> from gasp import * Traceback (most recent call last): File "", line 1, in from gasp import * File "C:\Python26\lib\site-packages\gasp\__init__.py", line 1, in from api import * File "C:\Python26\lib\site-packages\gasp\api.py", line 1, in import backend File "C:\Python26\lib\site-packages\gasp\backend.py", line 7, in except ImportError: raise 'Pygame is not installed. Please install it.' TypeError: exceptions must be old-style classes or derived from BaseException, not str >>> Can anybody help me with this? Thanks a lot P.S. SORRY FOR THE LONG EMAIL ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor