[Tutor] Help with Python

Dave Angel d at davea.name
Sun May 11 13:37:14 CEST 2014


On 05/10/2014 11:16 PM, Glen Chan wrote:
> Hello, I am a student trying to figure out Python. I am getting errors that I don't know how to fix. What do you do after you get the error message and something is highlighted? Does that have to be deleted? Anyway, here is what I mean...
>
>
> def main():
>      print
>      #initialize variables
>      playerOne = 'No Name'
>      playerTwo = 'No Name'
>
>      #call to inputNames
>      playerOne, playerTwo = inputNames(playerOne, playerTwo)
>      #while loop to run program again
>      while endProgram == 'no':
>          #initialize variables
>       winnersName = 'NO NAME'
>       p1number = 0
>       p2number = 0
>          #call to rollDice
>       winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)
>
>          #call to displayInfo
>       winnerName
>          endProgram = raw_input('Do you want to end program? (Enter yes or no): ')
>

When posting a question, you should always specify the Python version 
and OS, though it probably doesn't matter here.

As others have said, please paste the exact error message.  This is a 
text mailing list, so any highlighting you may have tried to include is 
lost on most of us.  Please post in a text message, not html, as many 
things can go wrong in the re-interpretation, especially in any source code.

I pasted your code into a text editor, saved the file, and ran it a 
terminal window in Python 2.7 under Linux,

davea at think2:~/temppython$ python glen.py
   File "glen.py", line 25
     endProgram = raw_input('Do you want to end program? (Enter yes or 
no): ')
     ^
IndentationError: unexpected indent
davea at think2:~/temppython$

As you can see the callstack shows the line that has a problem, and 
shows s a specific error message.  The problem is that you indented a 
line improperly.  You only add to the indentation within a function, an 
if/else statement statement block, class, with clause, etc.  Ordinary 
statemdents have to line up with the ones before, not counting comments.

If you line up the endProgram line with the previous winnername line, 
this particular error will go away.  But you have others.

I notice you mix spaces and tabs for indentation, which is dangerously 
confusing.  You should stick to one or the other, and I prefer spaces. 
I configured my text editor (emacs) to turn any tabs into 4 spaces, so 
that I won't get into trouble.

in some places you only indent by one space. That's hard to read and you
can wind up with problems from that.  Better to stick with 4, though 
some people seem to prefer 2.

Next problem is:

davea at think2:~/temppython$ python glen.py
   File "glen.py", line 44
     elif p1number > p2number:
                             ^
IndentationError: unindent does not match any outer indentation level

This error is because the elif clause does not line up with the if clause.

But a bigger problem in that portion of code is that you're apparently 
starting a new function, but never define it.  No def line follows the 
comment:  #this function displays the winner

If you add that def xxx():  line, and indent the if, then the elif will 
line up as expected.

After fixing that, the next error is:

Traceback (most recent call last):
   File "glen.py", line 51, in <module>
     main()
   File "glen.py", line 15, in main
     while endProgram == 'no':
UnboundLocalError: local variable 'endProgram' referenced before assignment


That comes because you have a while statement that refers to the 
variable endProgram, which has not been yet bound to a value.  You need
     endProgram = "no"
before that if statement.

In each of these cases, the error message tells you pretty closely 
what's wrong, and where.  You will need to learn to read the error 
messages, the tracebacks.

Your next problem is one of program logic, and I leave it to you.

-- 
DaveA


More information about the Tutor mailing list