[Tutor] Use of sqrt() from math module
Hi, I need to find the square root of a number in a program I am writing. I have imported the 'math' module so I thought I could just call sqrt(x) but I get an error message. Extact from my code and error message below. import sys, pygame, math ... if ypos >= 384 and velocity > 0: impactvel = sqrt(velocity ** 2 + (2 * gravity * (ypos - 384 * 160))) ... Traceback (most recent call last): File "", line 32, in ValueError: math domain error This one has me stumped as the usage of the module looks straight forward from the documentation. Thanks for looking. Matt ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Use of sqrt() from math module
Matt Smith wrote: > import sys, pygame, math > > ... > > if ypos >= 384 and velocity > 0: > impactvel = sqrt(velocity ** 2 + (2 * gravity * (ypos - 384 * > 160))) > > ... > > > Traceback (most recent call last): > File "", line 32, in > ValueError: math domain error Apologies, the actual error I get when I run the code above is: Traceback (most recent call last): File "", line 31, in NameError: name 'sqrt' is not defined The error I quoted first was when I tried to call 'math.sqrt(x)' Matt ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Use of sqrt() from math module
Matt, After using "import math" you will need to use the qualified name math.sqrt(blah) to call the square root function. That explains the NameError when trying to use the unqualified name, sqrt. As to your first message, the ValueError that you are reporting with the usage math.sqrt is likely due to an attempt to take the square root of a negative number (presumably because your (ypos - 384 * 160) factor is negative. With regard, Michael On Saturday December 1, 2007, Matt Smith wrote: >Matt Smith wrote: >> import sys, pygame, math >> >> ... >> >> if ypos >= 384 and velocity > 0: >> impactvel = sqrt(velocity ** 2 + (2 * gravity * (ypos - 384 * >> 160))) >> >> ... >> >> >> Traceback (most recent call last): >> File "", line 32, in >> ValueError: math domain error > >Apologies, the actual error I get when I run the code above is: > >Traceback (most recent call last): > File "", line 31, in >NameError: name 'sqrt' is not defined > >The error I quoted first was when I tried to call 'math.sqrt(x)' > >Matt > >___ >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] Use of sqrt() from math module
Matt Smith wrote: > > Apologies, the actual error I get when I run the code above is: > > Traceback (most recent call last): >File "", line 31, in > NameError: name 'sqrt' is not defined > > The error I quoted first was when I tried to call 'math.sqrt(x)' > > Matt > Apologies myself... I didn't read your whole message. Frankly, I don't know what's the problem in your program. Sorry. Ziyad. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Use of sqrt() from math module (Out of office)
I will be out of the office until Monday 10 December. If your request is urgent, please contact the helpdesk at: [EMAIL PROTECTED], alternatively, please dial: 0207 566 8771 Cheers Pierre ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Use of sqrt() from math module
Matt Smith wrote: > Hi, Hi... > > I need to find the square root of a number in a program I am writing. I have > imported the 'math' module so I thought I could just call sqrt(x) but I get > an > error message. Extact from my code and error message below. > > > import sys, pygame, math You import the module "math", not what's _inside_ it! (This is important! Both ways are okay, but in your case this is important!) > > ... > > if ypos >= 384 and velocity > 0: > impactvel = sqrt(velocity ** 2 + (2 * gravity * (ypos - 384 * 160))) > > ... Where did you get this thing called "sqrt"? You didn't define a function called "sqrt", did you? What you need is the "sqrt" function (or method) that lives _inside_ the "math" module, and you *must* tell that to Python, otherwise it won't find it! To fix your problem, either: - Use: "from math import sqrt". But this will import the "sqrt" function only. If all you need from the "math" module is this function/method then this is fine. (Though, this is generally not good practice, since you'll pollute you name space. The other solution is better.) - Or in the line that triggers the error, use: "math.sqrt(..." instead. By using "math." you tell Python where to find this thing that's called "sqrt", which is what you want. > > > Traceback (most recent call last): >File "", line 32, in > ValueError: math domain error I see you sent a new message with the actual error which is: Traceback (most recent call last): File "", line 32, in ValueError: math domain error which confirm what I wrote above. Hope this helps. Ziyad. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Use of sqrt() from math module
Michael H.Goldwasser wrote: > After using "import math" you will need to use the qualified name > math.sqrt(blah) to call the square root function. That explains the > NameError when trying to use the unqualified name, sqrt. > > As to your first message, the ValueError that you are reporting with > the usage math.sqrt is likely due to an attempt to take the square > root of a negative number (presumably because your (ypos - 384 * 160) > factor is negative. Thanks Michael and Ziyad, it seems I just had my brackets in the wrong place leading to trying to square root a number less than 0. Matt ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyQt segfault
On Sat, Dec 01, 2007 at 01:04:04AM +0100, David Boddie wrote: > Something like that, yes. The internal character data becomes invalid, but > there's still a reference to the QString object. > > Here's the original code posted by Tiago: > > > class Combobox(QtGui.QDialog): > > def __init__(self): > > QtGui.QDialog.__init__(self) > > self.ui = Ui_Dialog() > > self.ui.setupUi(self) > > > > self.connect(self.ui.comboBox, QtCore.SIGNAL("activated(QString)"), > > self.save) > > def save(self, qstring): > > # Here it works: > > #Aux.mystring = unicode(qstring) > > Aux.mystring = qstring > > The correct way to handle this is commented out: to take a copy of the data, > as you pointed out. This could be done by converting it to a Python unicode > object, as shown, or by copying the QString: > > Aux.mystring = QString(string) > > [Off topic, but PyQt-related: You need to explicitly copy value types with > PyQt because the semantics of copying objects with Python are different to > those for copying Qt's value classes in C++. In Python, you just bind an > object to a name, but the equivalent assignment in C++ is basically just > creating an additional reference to the same object.] If I understand that correctly, my Aux.mystring is pointing to the same object passed by QtCore.SIGNAL, which is being garbage-collected? But the reference in Aux.mystring should not be enough to keep the object around? Thanks, Tiago. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Selecting a browser
Ricardo Aráoz wrote: > Hi, I've checked webbrowser module and so far I find no way of selecting > a browser other than the default one. Say I want certain places opened > with IE and others with Mozilla, and I don't want to mess with the > user's setting of the default browser. Any tips? > TIA I think one would normally use the form webbrowser.get('firefox'), on unix systems. But if I understand correctly, the "problem" with the webbrowser module on windows (and perhaps it is similar on a mac) is that unless the program can be found on your system PATH, only a generic 'windows-default' browser class is registered, which uses os.startfile, releasing control to the os, and serves to open the url in the user's default browser. If you're determined to use the webbrowser module on windows, you might be able to do something like this: import webbrowser ffcommand = "c:/program files/mozilla firefox/firefox.exe %s &" ff = webbrowser.get(ffcommand) ff.open("http://www.example.com";) iecommand = "c:/program files/internet explorer/iexplore.exe %s &" ie = webbrowser.get(iecommand) ie.open("http://www.example.com";) I suppose you could also register them manually for later use with the webbrowser.get(browser_name) form. webbrowser.register('firefox', None, ff) webbrowser.get('firefox').open('http://example.com') Personally, I would probably just cut out the middle module and use subprocess.Popen to start the browser, after checking if it is installed (with os.path.isfile, or similar) -- which seems to be, more or less, what the webbrowser module does if it finds one of the predefined browsers on your system PATH. Something like this (pseudo-code): browser = 'c:/program files/mozilla firefox/firefox.exe' if os.path.isfile(browser): p = subprocess.Popen([browser, 'http://www.example.com']) # if you want to wait for the browser to # close before continuing use p.wait() here else: ... web browser not found ... For dispatching based on site (url, or some other criteria), one idea would be to wrap something like the above in a function which accepts the web browser program path as an argument, and then pass the function a path appropriate for the given criteria. Here is another (untested) example to demonstrate: import subprocess import urlparse import sys, os FFPATH = 'c:/program files/mozilla firefox/firefox.exe' IEPATH = 'c:/program files/internet explorer/iexplore.exe' IESITES = ['microsoft.com', 'www.microsoft.com'] def launch(url, browser, wait=False): if os.path.isfile(browser): p = subprocess.Popen([browser, url]) if wait: p.wait() else: print 'Invalid browser.' def main(url): # pick browser path by domain name netloc = urlparse.urlparse(url)[1] if netloc in IESITES: launch(url, IEPATH) else: launch(url, FFPATH) if __name__ == '__main__': if sys.argv[1:]: main(sys.argv[1]) else: print 'Not enough arguments.' In theory, if you run this script from a console on windows with any microsoft.com url as an argument, it should open in IE -- where all others open in firefox. Really rough, but I hope it helps. Regards, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor