[Tutor] Simple Python Program

Evert Rol evert.rol at gmail.com
Sat Jul 31 10:50:13 CEST 2010


> Can anyone tell me how to fix the errors I am getting if possible? I'm quite new and so confused...

You're not telling us what the errors are. If you'd like us help to debug your program, best is to provide information about how you run it, and what output you're getting. Not just the program (sure, we can all copy-paste the program and run it, but we're not all going to do that). Just copy-paste the error message along with the rest of the email a next time.
Python is pretty verbose and clear with its error messages; once you learn to read the traceback (bottom-up), you can often figure out what's wrong: read the exact error message and line number, and try to understand why you get that message.

That said, I can see one glaring error when skimming through your code (unless I'm horribly misreading things):
 
>  import random
> def main():
>     print
> 
>     playerOne, playerTwo = inputNames(playerOne, PlayerTwo)
>      
>     while endProgram == 'no':

You use endProgram to compare with 'no' here, but
> 
>         endProgram == no

you only set the variable here.

This is programming 101 (not even Python specific): declare (& set) your variables before you use them (in comparisons, function calls, etc).

Also:
>         playerOne = 'NO NAME'
>         playerTwo = 'NO NAME'

are set here to 'NO NAME' (some default value), but *before* that, you assign real names. Since the default assignment is after the proper assignment, you end up with 'NO NAME' for both players (except that your program currently crashes at the while statement).

Finally:
> 
> also how do I display the winner under the displayInfo function?
 
If you want to do that, you'll have to feed the displayInfo() function the name of the winner as an argument.
What you do below in the second line, is assign the displayInfo function to winnerName. Instead, you'll have to call that function, with the winnerName as as argument (you're doing this correctly before, so it's somewhat odd you're doing it wrong here).

>         winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)
>         winnerName = displayInfo

All in all, I can see why you're "so confused". Do you understand what you're doing; specifically, do you understand the flow of the program? Or did you just copy-paste something and made a few edits, then gave it a try? 
Perhaps you should take a step back, start even a bit smaller (single function programs), read a few other tutorials, then come back to this program.
You're most welcome to ask questions here, but looking through your code, there's a few generic programming mistakes here that you should learn about first.

Good luck,

  Evert


>         endProgram = raw_input('Do you want to end program? (Enter yes or no): ')
> 
> def inputNames(playerOne, PlayerTwo):
> 
>     p1name = raw_input("Enter your name.")
>     p2name = raw_input("Enter your name.")
>     return playerOne, playerTwo
> 
> def random(winnerName):
> 
>     p1number = random.randint(1, 6)
>     p2number = random.randint(1, 6)
> 
>     if p1number > p2number:
>         playerOne = winnerName
>     elif p1number < p2number:
>         playerTwo = winnerName
>     else p1number == p2number:
>         playerOne, playerTwo = winnerName
>         
> def displayInfo():
> 
>     #how do I display winner?
>     
> main()
> 



More information about the Tutor mailing list