Re: [Tutor] broken script - curiouser and curiouser
On Thursday 07 July 2011 00:48:24 Alan Gauld wrote: > "Lisi" wrote > > >> : File "ex26.py", line 10 > >> : def print_first_word(words) > >> : ^ > >> : SyntaxError: invalid syntax > >> : lisi@Tux:~/Python/LearnPythonTheHardWay$ > >> > >> Look at the error. Look at the error carefully. > > > > I could see nothing that was different. > > The error report says you had no colon in your code. > Python couldn't see it... But there *was* a colon. So why couldn't python see it? > Are you sutre you didn't add it before posting but > after running the code? Positive. > Or are you testing it by impotrting a module? > In that case the interpreter may still be using > the old version because you haven't reloaded > the updated version? No > > Yes - it was one of the first things I looked for when > > the error came up (that and check that the brackets > > were right). It was there in what seemed to me > > to be the right position. > > In that case I suspect the module theory. > How are you running the code? $ python ex14.py > > In the end, having tried absolutely everything else > > I could think of, and given that the error arose at > > the closing bracket at the end of line 10, I > > tried the only thing I could remotely think of that > > I hadn't tried and put a space in between the > > second bracket and the colon - and the wretched thing > > ran. > > As you know that shouldn't have made any difference. > Did you try going back and removing the space again > and see if the error came back? I now have at your suggestion, and it runs. So I am now still more mystified. As I say I had tried retyping it (and checking carefully) umpteen times and also copied and pasted from a succesful function definition and checked the brackets and colon every time. > > You could obviously see something wrong > > The posted error report had no colon. Yes - but the script had one, which was why I included the actual script in my original post. I seem to have cut too much in this email! Thanks, Alan. Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
Hi Lisi On 7 July 2011 08:52, Lisi wrote: > > > You could obviously see something wrong > > > > The posted error report had no colon. > > Yes - but the script had one, which was why I included the actual script in > my > original post. I seem to have cut too much in this email! > The error message from Python is quoting what the interpreter read/saw from the script file it was running. If it quoted no colon, then there was no colon in the python file read by the interpreter, at the time that it tried to run it. Your job is to figure out how this has happened. Do you maybe have several copies of the script lying around in different folders? Were you maybe editing the the file (and keeping it open in the editor) while running it from a command prompt every so often and did you maybe not save the change before running the script (so that there may have been a discrepancy between the script on disk that was run by the interpreter and the script in your editor window)? Cheers Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On Thursday 07 July 2011 10:21:59 Walter Prins wrote: > Hi Lisi > > On 7 July 2011 08:52, Lisi wrote: > > > > You could obviously see something wrong > > > > > > The posted error report had no colon. > > > > Yes - but the script had one, which was why I included the actual script > > in my > > original post. I seem to have cut too much in this email! > > The error message from Python is quoting what the interpreter read/saw from > the script file it was running. If it quoted no colon, then there was no > colon in the python file read by the interpreter, at the time that it tried > to run it. Your job is to figure out how this has happened. Do you maybe > have several copies of the script lying around in different folders? No. KWrite keeps an automatic backup, but I never use it. As a result of your question, I have just run the automatic backup. It runs fine. > Were > you maybe editing the the file (and keeping it open in the editor) while > running it from a command prompt every so often and did you maybe not save > the change before running the script I did do that once or twice during the long saga, but immediately realised and saved the file before running again. In fact, I think that it must sometimes have been saved multiple times because I was so afraid that I would do that. > (so that there may have been a > discrepancy between the script on disk that was run by the interpreter and > the script in your editor window)? I can see nothing that could have caused it. :-( Believe me, I thought of all the possibilities that have been mentioned so far and tried them out, with no joy. The only thing that worked was inserting a space and then, at Alan's suggestion, removing it. I am completely baffled. There must have been something, but I cannot fathom what. What difference does inserting a space and then deleting it make? At least this confirms that I took all the right debugging steps. I think I just have to write it down as one of life's little mysteries. If I can't uncover what happened with the files in front of me, clearly people who can see neither the screen nor the files have an impossible task! I am very grateful for all the list's help. I may have been left with the puzzle, but I have learnt a lot in the process of trying to debug the script. Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On Thu, Jul 7, 2011 at 3:15 AM, Lisi wrote: > There must have been > something, but I cannot fathom what. What difference does inserting a space > and then deleting it make? > I wonder whether you might have had a stray non-printing character in there? I believe that that would explain the symptom; of course, I have no idea how it would have got there, so this merely pushes the mystery down a layer! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On Thursday 07 July 2011 11:46:01 Marc Tompkins wrote: > On Thu, Jul 7, 2011 at 3:15 AM, Lisi wrote: > > There must have been > > something, but I cannot fathom what. What difference does inserting a > space > > > and then deleting it make? > > I wonder whether you might have had a stray non-printing character in > there? I believe that that would explain the symptom; of course, I have no > idea how it would have got there, so this merely pushes the mystery down a > layer! Low be it whispered I thought of that and copy-pasted into a WP and turned on non-printed characters. There were none. Only those that were there advisedly. One day when I am even older and even greyer I may look at this and a light may dawn and say: "So *that's* what was happening." Thanks for the input! Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On Thursday 07 July 2011 10:21:59 Walter Prins wrote: > Do you maybe > have several copies of the script lying around in different folders? I _think_ that you may have the answer here - tho' for me it raises almost as many questions as I had in the first place. The exercise I was having that intractable problem with was ex25. ex26 involves downloading a file from the Internet and finding and correcting the errors. In error, I downloaded ex26 before I had done ex25. So I left it and did ex25. Having finally given up on ex25, I am now doing ex26. And guess what?! Line 10 has a missing colon. So _how_ did a missing colon in ex26 cause problems in ex25 Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python conundrum
Hi! :-) >From the book I am working from: A shortcut is to do your import like this: from ex25 import * Is this a new and wonderful meaning of the word "shortcut"? _Why_, _how_ is that a shortcut for: import ex25 when it has seven *more* characters (counting the spaces)? Or, put another way, twice as many words? :-/ It is obviously an alternative, but I just don't get that it is a shortcut. Thanks, Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python conundrum
if you type from ex25 import * then you don't have to put ex25. in front of the functions. On Thu, Jul 7, 2011 at 5:17 PM, Lisi wrote: > Hi! :-) > > >From the book I am working from: > > A shortcut is to do your import like this: > from ex25 import * > > Is this a new and wonderful meaning of the word "shortcut"? _Why_, _how_ > is > that a shortcut for: > > import ex25 > > when it has seven *more* characters (counting the spaces)? Or, put another > way, twice as many words? :-/ > > It is obviously an alternative, but I just don't get that it is a shortcut. > > Thanks, > Lisi > ___ > 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] Python conundrum
On Thu, Jul 7, 2011 at 3:17 PM, Lisi wrote: > Hi! :-) > > >From the book I am working from: > > A shortcut is to do your import like this: > from ex25 import * > > Is this a new and wonderful meaning of the word "shortcut"? _Why_, _how_ is > that a shortcut for: > > import ex25 > > when it has seven *more* characters (counting the spaces)? Or, put another > way, twice as many words? :-/ > > It is obviously an alternative, but I just don't get that it is a shortcut. > > Thanks, > Lisi > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Consider this - Say I had a program that used a few functions from the math module - import math print math.sqrt(4) print math.pow(2,2) print math.factorial(5) print math.cos(30) Because the way import works, I would have to write math (to say "take from the namespace math") before each function from math. Using from math import *, I can instead write - from math import * print sqrt(4) print pow(2,2) print factorial(5) print cos(30) This imports the functions directly into the __main__ namespace Now, over the course of my program, this would save hundreds of characters, as you wouldn't need to type "math" before to state which namespace to use. (Of course, if using only certain functions from a module such as math, you should really use from math import sqrt, pow, factorial, cos) - notice >>> import math >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'math'] >>> from math import * >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil ', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frex p', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radian s', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] >>> from math import factorial, pow, sqrt, cos >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'cos', 'factorial', 'pow', 'sqrt'] HTH. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
Michael M Mason wrote on 07 July 2011 at 15:38:- > > In error, I downloaded ex26 before I had done ex25. So I left > > it and did ex25. > > > > Having finally given up on ex25, I am now doing ex26. And guess > > what?! Line 10 has a missing colon. > > > > So _how_ did a missing colon in ex26 cause problems in ex25 > > Maybe you've been editing ex25 and then executing ex26. That'd get you > the same error in the same place over and over again. Looks like this might be it. In your earlier post the error reported is in ex26:- : lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex26.py : File "ex26.py", line 10 : def print_first_word(words) : ^ : SyntaxError: invalid syntax : lisi@Tux:~/Python/LearnPythonTheHardWay$ -- Michael This mail was sent via Mail-SeCure System. This footnote confirms that this email message has been scanned by PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On 07 July 2011 at 15:07 Lisi wrote: > In error, I downloaded ex26 before I had done ex25. So I left > it and did ex25. > > Having finally given up on ex25, I am now doing ex26. And guess > what?! Line 10 has a missing colon. > > So _how_ did a missing colon in ex26 cause problems in ex25 Maybe you've been editing ex25 and then executing ex26. That'd get you the same error in the same place over and over again. -- Michael This mail was sent via Mail-SeCure System. This footnote confirms that this email message has been scanned by PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On Thursday 07 July 2011 15:42:12 Michael M Mason wrote: > > Maybe you've been editing ex25 and then executing ex26. That'd get you > > the same error in the same place over and over again. > > Looks like this might be it. In your earlier post the error reported is in > ex26:- > > : lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex26.py > : File "ex26.py", line 10 > : def print_first_word(words) > : ^ > : SyntaxError: invalid syntax > : lisi@Tux:~/Python/LearnPythonTheHardWay$ Doh! Thank you , Michael. The book does say something along the lines of "If you are stuck, leave it, take a break and come back to it later". Still, at least I can learn from my mistakes. ;-) That is my story anyway, and I'm sticking to it. Many years ago, in the days of the ark, when men were real men, and programming meant writing strings of 0s and 1s and then shifting pins around a board, I once spent over two hours trying to discover what was wrong with my code in just one spot. At the end of the over 2 hours it finally dawned on me that my code was fine - the memory location for that particular pin was faulty. Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python conundrum
On Thu, Jul 7, 2011 at 7:30 AM, Noah Hall wrote: > On Thu, Jul 7, 2011 at 3:17 PM, Lisi wrote: > > Hi! :-) > > > > >From the book I am working from: > > > > A shortcut is to do your import like this: > > from ex25 import * > > > > Is this a new and wonderful meaning of the word "shortcut"? > I haven't read the book in question, so I don't know whether they cover this in the next paragraph... but this is generally BAD PRACTICE. If the module you're importing contains functions that have the same names as functions in the standard library, or in another module that you're also importing, then _the last thing you import prevails._ If that was really what you wanted, well and good - but it's very, very easy to make mistakes this way, and frustratingly hard to catch them. To take an extreme example, imagine that you're importing a module called "bogus", and that "bogus" contains a function called "int()" function which actually returns a random number. (Why? 'Cause I'm making a point here, that's why!) In this hypothetical example, if you import "bogus" in the usual way - > import bogus > int("5") > >> 5 > bogus.int("5") > >> 97 > you get what you'd expect. But if you use the nifty "shortcut" method (S much easier!) you might get the following: > import * from bogus > int("5") > >> 63 > Huh? > And again, if that's what you want, more power to you. But I prefer to overload methods explicitly so that I know what code will run when I call a function. To be clear: most modules don't randomly overload standard methods... much. One common exception is str(); many classes offer custom str() methods, and calling a custom method when you're expecting the standard one can waste your whole day. In the words of the great philosopher "import this" - "Namespaces are one honking great idea -- let's do more of those!" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python conundrum
"Lisi" wrote A shortcut is to do your import like this: from ex25 import * Is this a new and wonderful meaning of the word "shortcut"? No, it just saves you typing ex25. in front of every name in the module. Of course, the "import *" technique is frowned upon because of the risk of introducing name conflicts, so its not really a recommended "shortcut"... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _Why_, _how_ is that a shortcut for: import ex25 when it has seven *more* characters (counting the spaces)? Or, put another way, twice as many words? :-/ It is obviously an alternative, but I just don't get that it is a shortcut. Thanks, Lisi ___ 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] broken script - curiouser and curiouser
>Many years ago, in the days of the ark, when men were real men, and So men in the present day are fake men? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
TWO HOURS?? You aren't a Real Programmer (http://www.catb.org/jargon/html/story-of-mel.html) until you've spent a whole day debugging something trivial. Heck, I quite recently spent a day plus trying to figure out why my microcontroller project wasn't working right. I was using a command line tool to tell it what functions to execute, settings to set, etc. But it didn't produce any sensible output. No communication errors were reported, and the code looked fine. It even echoed back the updated settings correctly. I finally realized that each invocation of the command line tool opened and closed the serial port, which had the effect of resetting the microcontroller! So, anything saved in volatile memory was naturally lost! Ok, maybe that was just plain old boneheadery. Cheers On Thursday 07 July 2011, Lisi wrote: > On Thursday 07 July 2011 15:42:12 Michael M Mason wrote: > > > Maybe you've been editing ex25 and then executing ex26. That'd get you > > > the same error in the same place over and over again. > > > > Looks like this might be it. In your earlier post the error reported is > > in ex26:- > > > > : lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex26.py > > : File "ex26.py", line 10 > > : def print_first_word(words) > > : ^ > > : SyntaxError: invalid syntax > > : lisi@Tux:~/Python/LearnPythonTheHardWay$ > > Doh! Thank you , Michael. The book does say something along the lines of > "If you are stuck, leave it, take a break and come back to it later". > > Still, at least I can learn from my mistakes. ;-) That is my story anyway, > and I'm sticking to it. > > Many years ago, in the days of the ark, when men were real men, and > programming meant writing strings of 0s and 1s and then shifting pins > around a board, I once spent over two hours trying to discover what was > wrong with my code in just one spot. > > At the end of the over 2 hours it finally dawned on me that my code was > fine - the memory location for that particular pin was faulty. > > Lisi > > ___ > 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