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

Sean Abrahams Sean Abrahams <sa@sfsu.edu>
Mon Feb 3 18:18:01 2003


I'm not an expert programmer, but I noticed right away that instead of
making 'item' a function, you could simply make it a list (or tuple,
since the selections are finite; you can only select rock, paper, or
scissors)

So:

>def item( a ):                                          # Convert int to string
>    if a == 1:
>        return "Rock"
>    elif a == 2:
>        return "Paper"
>    elif a == 3:
>        return "Scissors"

becomes:

item = ('Rock','Paper','Scissors')

However, this presents a problem. Since tuples/lists start counting
from 0, your selections won't match up. Meaning, in your example item
function, Rock == 1, but in my item tuple example Rock == 0.

So, here's a good question for the more experienced programmers out
there. Is it better to still use a list/tuple and just put an empty
value for item[0] (Assuming we want to keep the UI the same, 1) for
the user to pick Rock, 2) for Paper, etc.) Or, would it be better to
use a dictionary like so?

item = {1 : 'Rock', 2 : 'Paper', 3 : 'Scissors'}

Then reference the values using item[n]?

--Sean Abrahams
(Not 'Shaleh' Perry)

Monday, February 3, 2003, 2:41:07 PM, you wrote:

a> Sean, Magnus, Alan, Bob and Gregor

a> At the risk of belabouring this thread, I wanted to update you with
a> developments on my earlier requests for input and since you all so
a> graciously contributed something to helping me, I wanted to share where
a> it is at (to date).
a> I utilised Magnus's idea regarding turning the rock, paper, scissors
a> combination into a dictionary, and calling the key/value pair to
a> ascertain which wins.
a> I also wanted to allow the user to keep the game going rather than just
a> play once and have to press F5 again to start it off, which is why I
a> created the game as a module itself that can just be called.
a> Finally, I wanted to provide a little more feedback to the user - hence
a> all the stuff about the round summary.
a> I was hoping that I might be able to carry a round winner's total over
a> from one game to the next to give a series summary upon quitting, but
a> haven't figured out how to do that yet.
a> Anyway, have attached the code, and would naturally welcome comments.
a> It is probably too many lines for what the program actually does, so if
a> you have any ideas how parsimony can be achieved, please share them with
a> me, but I'd also appreciate it if you could explain what it is that you
a> are actually doing too - that would aid my learning.

a> Thanks folks.
a> Kind regards
a> Andrew