Cannot figure out line of code, also not understanding error

Chris Angelico rosuav at gmail.com
Thu Feb 20 03:03:36 EST 2014


On Thu, Feb 20, 2014 at 6:32 PM, ApathyBear <nirchernia at gmail.com> wrote:
> 1. What does this line of code mean:
> return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
>
> Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.

It's supposed to be calling Athlete() with some arguments. However,
due to the extra open parenthesis between the keyword "return" and the
rest, it...

> 2. Why am I getting this error when I try to run the file?
> PS C:\Users\N\desktop> python gg.py
>   File "gg.py", line 34
>     except IOError:
>          ^
> SyntaxError: invalid syntax
> PS C:\Users\N\desktop>

... causes this error, which is detected at the point where a keyword
comes in that makes no sense inside the return expression.

The solution is to delete the first open parenthesis:

return Athlete(temp1.pop(0),temp1.pop(0), temp1)

Then you have properly matched parens, and it should carry on happily.

Tip: Computers report errors where they find them, which isn't always
where the error actually is. But with most modern programming
languages, the file is read from top to bottom and left to right, so
when a problem is reported, its cause is always *before* it in the
file. Never after it. You could make a horrible mess of the file after
that "except" and you wouldn't change the error. (Apart from things
like text encoding, which are done in a separate pass.) You'll get
used to scanning up a line or two from the error to find the real
cause.

ChrisA



More information about the Python-list mailing list