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

Sean Abrahams Sean Abrahams <sa@sfsu.edu>
Mon Feb 3 19:13:02 2003


Solution to my own question.

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

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

You can reference it as item[n-1].

--Sean

Monday, February 3, 2003, 3:20:35 PM, you wrote:

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

SA> So:

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

SA> becomes:

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

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

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

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

SA> Then reference the values using item[n]?

SA> --Sean Abrahams
SA> (Not 'Shaleh' Perry)

SA> 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


SA> _______________________________________________
SA> Tutor maillist  -  Tutor@python.org
SA> http://mail.python.org/mailman/listinfo/tutor