[Tutor] rock, paper, scissors (ro sham bo) part deux

Magnus Lycka magnus@thinkware.se
Mon Feb 3 18:42:01 2003


At 22:41 2003-02-03 +0000, ahimsa wrote:
>Sean, Magnus, Alan, Bob and Gregor
>
>At the risk of belabouring this thread, I wanted to update you with
>developments on my earlier requests for input and since you all so
>graciously contributed something to helping me, I wanted to share where
>it is at (to date).

Nice! Keep on going!

I'll just like to show how a few lines of code code could
look even better. Don't forget to use descriptive variable
names, and for style issues, have a look at the style guide:
http://www.python.org/peps/pep-0008.html


# "Value" wins over "Key"
winner = { "Rock" : "Paper",
            "Scissors" : "Rock",
            "Paper" : "Scissors" }

I think winner is a more descriptive name than options. I also think
it's clearer with one entry per line. I also think the changed comment
helps to understanf the purpose of the dict better.

menu = ["Rock", "Paper", "Scissors"]

def item( choice ):
     'Translate menu choice 1,2,3 to descriptive string'
     return menu[choice - 1]

For me, this is also clearer than the if-statement. It's certainly shorter.
You can also use the menu list when you create the menu that you present
to the user. That way you can change order between the items in just one
place if you would suddenly want scissors first etc.

def whoWins( player1, player2 ):
     'Value in winner dictionary wins over its key'
     if winner[player1] == player2:
         print player2, "beats", player1, "- You lost!"
         return player2
     elif winner[player2] == player1:
         print player1, "beats", player2, "- You win!!"
         return player1
     else:
         print "A tie - play again"

...

             winnerThisTurn = whoWins(player, computer)

             # keep score
             if computer == winnerThisTurn :
                 compWin += 1
             elif player == winnerThisTurn :
                 playWin += 1

Don't determine who won twice, and don't forget to use
descriptive names. You will get used to typing fast in due
time...

>I was hoping that I might be able to carry a round winner's total over
>from one game to the next to give a series summary upon quitting, but
>haven't figured out how to do that yet.

You need another (outer) loop.

numberOfTurns = 3

# Total score
totComp = totPlayer = 0

while True:
     # Score this round
     compWin = playWin = 0

     for i in range(numberOfTurns):

     ...

     if playWin > compWin:
         print "You won that round"
         totPlay += 1
     elif compWin > playWin:
         print "The Computer won that round"
         totComp += 1
     else:
         print "That round was a tie"

     ...
     if not wantToPlayMore():
         break

>Anyway, have attached the code, and would naturally welcome comments.
>It is probably too many lines for what the program actually does, so if
>you have any ideas how parsimony can be achieved, please share them with
>me, but I'd also appreciate it if you could explain what it is that you
>are actually doing too - that would aid my learning.

There are a few places where you repeat code, and that's better broken
out to separate functions or centralized to one place in the code. For
instance you calculate who wins twice. Once for the message, and once for
the score keeping. This is error prone, you might change it in one place
because you came up with some improvement and forget the other place, and
end up with the score and the messages being out of sync. See above.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se