[Tutor] Issue With a Python Game

Walter Prins wprins at gmail.com
Sun Apr 14 23:16:05 CEST 2013


Hi John,

On 10 April 2013 15:21, John Bochicchio <luckytsukune at gmail.com> wrote:

> I have a question about a game I am making. I finished the overall code,
> but keep getting errors when I try to play. This is my most common error:
> C:\Users\John\Documents\Python Games\Noah>python nickeladventuredemo.py
> Traceback (most recent call last):
>   File "nickeladventuredemo.py", line 451, in <module>
>     main()
>   File "nickeladventuredemo.py", line 78, in main
>     levels = readLevelsFile('levels.txt')
>   File "nickeladventuredemo.py", line 371, in readLevelsFile
>     assert startx != None  and starty!= None, 'Level %s (around line %s)
> in %s i
> s missing a "@" or "+" to mark the start point.' % (levelNum+1, lineNum,
> filenam
> e)
> AssertionError: Level 1 (around line 10) in levels.txt is missing a "@" or
> "+" t
> o mark the start point.
>
> The error has something to do with the levels. I will attach the code and
> levels, as they are fairly long.
>

Firstly please next time include a segment of the failing code in the email
-- not everyone is prepared to or can be bothered to download and wade
through a large source file just to get to the bottom of an program error.

Secondly, re your your first assertion statement: it assumes that the
problem is due to a problem in the levels.txt file, which is not the case
here.  You'd do better IMHO to maybe not be so creative/presumptuous with
 the error message, and rather have it report exactly what the actual
assertion conditions (and maybe variable values at the time) is that failed
(in this case values of startx, starty.)   You might also consider rather
using Exceptions here.

All that said, the problem is you have the code throwing the exception at
the wrong level of indent.  You need to de-indent all the assert statements
as well as the code that follows up to but excluding the return statement
so that it's at the same level of the first "for" loop.  (Differently put,
the reason the assertion is likely failing is because you're doing your
asserts before the loops have finished processing/reading your mapObj. You
presumably want the asserts once the mapObj's been finished processing by
the loop.)

As an aside,  the next failure you'll likely run into is the third assert,
which expects len(stars) >= len(goals), which I assume will fail as you are
not reading anything into "stars" list anywhere as you do for  goals.

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130414/8c488fa9/attachment.html>


More information about the Tutor mailing list