Phantom Errors - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Phantom Errors (/thread-22084.html) |
RE: Phantom Errors - prospero - Oct-31-2019 The plot thickens...... It seems to me that the spook errors occur when I paste in code taken from files I have previously made using UltraEdit-32 9.10 I'm presently using v 26 10.0.30 I have submitted a tech query to IDM RE: Phantom Errors - ichabod801 - Oct-31-2019 I would check the line endings and tabs carefully. I gotta say, I used to be a huge UltraEdit fan. But I tried out the latest version a few months ago, and after a few weeks I went right back to Sublime Text. RE: Phantom Errors - prospero - Oct-31-2019 I copied this function from another file. It causes not errors in it's original location. (It's part of a class) def IsValidEnemy (self, entity): me=Bladex.GetEntity(self.Name) if not entity.Person: return FALSE if entity.Kind=="LookoutMan": return FALSE if not entity.Life > 0: return FALSE if entity.Name == self.Name: return FALSE if entity.Kind==me.Kind: return FALSE if IsHoldingBow(entity.Name): return FALSE if CheckIfEnemy(entity.Name): return TRUE if entity.Name == "Player1": return TRUE return FALSEBut it doesn't like it. Traceback (innermost last): File "Cfg.py", line 98, in ? execfile("DefFuncs.py") File "DefFuncs.py", line 21, in ? import EnemyNPCTypes SyntaxError: 'return' outside function (line 1061)** I am now using Notepad++ RE: Phantom Errors - prospero - Nov-01-2019 It gets worse...... I am now getting bogus errors on anything I try to edit. I desperation I copied an entire file from my old computer and pasted it in place of the file I was editing. It all works fine now. I will have to somehow try to migrate my old UltraEdit onto my new PC. I've been writing Python for the best part of 20 years and never had a this before. It's tricky enough keeping everything working when you can see the code, but when it looks (and is) correct as it appears in the editor but has some invisible errors it's a bit harder. Sorry to rant..... Going for a kip now. RE: Phantom Errors - nilamo - Nov-01-2019 That sort of sounds like spaces are being converted to tabs (or the other way around), but I think the error you get would be indentation errors or "inconsistent use of whitespace". It also kind of looks like the error you'd get if the line previous was missing a closing parenthasis, but that shouldn't happen just from copy/paste. How are you running it? Is it possible that there's multiple versions of python involved, like it works in 2.x but the other program is using 3.x? RE: Phantom Errors - prospero - Nov-02-2019 I've always been very particular in my coding style and always use a single tab for indentation. Tidy code is easy-to-read code is my motto. It's Python 2 all the way I'm afraid as the game I am modding comes with it built in. I seem to remember years ago when I started editing Python files I used Notepad which is fine. Then when the files got too big I used Wordpad, which exhibited similar symptoms - perfectly sound code as written but wouldn't run. That's when I got UltraEdit and the rest is history...... RE: Phantom Errors - newbieAuggie2019 - Nov-02-2019 (Nov-02-2019, 02:23 AM)prospero Wrote: I've always been very particular in my coding style and always use a single tab for indentation. Hi! I'm just a newbie, so I'm not sure if this could help you. I saw this in PEP8: Tabs or Spaces? Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended! Source: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces Depending on your version of Python, maybe you could import and use the module tabnanny and the following function to detect ambiguous indentation in a file: tabnanny.check(file_or_dir) https://docs.python.org/2/library/tabnanny.html#module-tabnanny https://docs.python.org/3.3/library/tabnanny.html?highlight=indentation#module-tabnanny for instance, for both Python 2.7.17 and Python 3.3.7. (Changing the version on the top of the page, you can look for your specific version). The tabnanny.check() function checks the file or directory that you pass to as an argument, and detects ambiguous indentation. All the best, RE: Phantom Errors - prospero - Nov-02-2019 That's very interesting. Thanks for posting. The problem does seem to be with the indentation. What I can't get my head around is you can't see an error when the code is displayed in the editor. And why after coding for many years without any problem of this nature does it manifest itself now? RE: Phantom Errors - prospero - Nov-02-2019 Look at this code....... def MyGetEnemyName (self): me=Bladex.GetEntity(self.Name) char=Bladex.GetEntity("Player1") if not self.AttackOnSight: return None if not self.NameOfEnemy or Bladex.GetEntity(self.NameOfEnemy).Life <=0: pjp = me.Position enemyList=[] pj_dir=me.Rel2AbsVector(0.0,-1.0,0.0) for ename in Bladex.GetEnemiesVisibleFrom(pjp,30000,pj_dir,0.0): ene=Bladex.GetEntity(ename) if ene.Person: if self.IsValidEnemy(ene): enemyList.append(ene.Name) if me.CanISee(char): enemyList.append("Player1") if len(enemyList) == 0: return None enemyList.sort(self.ChooseCanISeeNearest) self.NameOfEnemy=enemyList[0] enemy=Bladex.GetEntity(self.NameOfEnemy) me.SetEnemy(enemy) self.ChaseCount=0 return self.NameOfEnemyIt's all over the place. Trouble is, in the editor it appears correct. This is how it should look...... def MyGetEnemyName (self): me=Bladex.GetEntity(self.Name) char=Bladex.GetEntity("Player1") if not self.AttackOnSight: return None if not self.NameOfEnemy or Bladex.GetEntity(self.NameOfEnemy).Life <=0: pjp = me.Position enemyList=[] pj_dir=me.Rel2AbsVector(0.0,-1.0,0.0) for ename in Bladex.GetEnemiesVisibleFrom(pjp,30000,pj_dir,0.0): ene=Bladex.GetEntity(ename) if ene.Person: if self.IsValidEnemy(ene): enemyList.append(ene.Name) if me.CanISee(char): enemyList.append("Player1") if len(enemyList) == 0: return None enemyList.sort(self.ChooseCanISeeNearest) self.NameOfEnemy=enemyList[0] enemy=Bladex.GetEntity(self.NameOfEnemy) me.SetEnemy(enemy) self.ChaseCount=0 return self.NameOfEnemyI cut each line in turn and set the cursor to the left margin. Did the required number of tabs and pasted it back in. It works fine now. This process could get a wee bit tedious though. And I might write code and it may be that it raises no errors, but how can I be sure it hasn't shifted a clause sideways and upset my logic???????? I can't keep checking it on this page. RE: Phantom Errors - ichabod801 - Nov-02-2019 This really sounds like a tabs vs spaces problem. Ultraedit has settings for making the white space characters visible, so you can tell where there are spaces and where there are tabs. Have you turned that on and looked at the code that is giving you errors? |