Would like feedback on Rock, Paper, Scissors script.

I'm in the process of teaching myself python 2.7, so feedback would be much appreciated. I'm mainly looking for ways to make the script more efficient, pythonic, or readable. Any other feedback is welcome, though.

https://gist.github.com/anonymous/a92061aa398d27eda716

Edit: Updated the gist.

Posted Mar 2, 2016, 6:34 PM UTC · edited

1414
blacwidonsfw•

Break the habit of using globals now. You should never need them. I don't think I have used them once in the last 3 years.

8
n1ywbn1ywb•

Yes. Encapsulate state in an object.

2
BlogtiemOP•

Done. Thanks for the input.

1
coding2learn•

One thing you could do is store the rules of the game in a dictionary.

rules = {'rock':'scissors', 'scissors':'paper', 'paper':'rock'}

This will save you having to code the comparisons with nested if statements.

Instructional resource I made here

6
JohnnyJordaan•

First of all, why bother with a seperate function to call repeatedly? Just keep everything outside a function or let the function loop 10 times inside itself. It saves you needing Globals (nasty) or other ways of 'keeping score'.

choices = ['Rock', 'Paper', 'Scissors']
UserScore = 0
ComScore = 0
while UserScore + ComScore < 10:

Then loop until User made a correct entry:

    while True:
        try:
            print "Choose 'Rock', 'Paper', or 'Scissors'."
            entry = raw_input("> ")
            print "\n"
            #force the first letter as a Capital
            UserDraw = choices.index(entry.capitalize())
        except ValueError:
            continue
        else:
            break

UserDraw is now with certainty the index of the choice, so no need for string evaluation. If you consider the sequence of choices, every Tie happens when the indexes match. Then the user wins if the index of computer is one less, but rotated of course as you can't have the index of -1.

i   0 1 2
    R P S
0 R T L W
1 P W T L
2 S L W T

That can be fixed if you consider the computer's index one more and rotating the last index (Scissors) to 0, thereby placing a Win on the situation that both indexes match:

i   0 1 2   
    S R P 
0 R W T L 
1 P L W T 
2 S T L W 

If you don't see it directly: the S column has gone from right to left and R and P shifted one to the right. In the original table the T (Ties) were aligned on matching indexes, now the W (Wins by User) are aligned on the same condition. That implemented, combined with indexing choices to print the choice of the computer results in:

    #slightly better range by using choices' length
    ComDraw = random.randint(0, len(choices))
    result = 'The computer chose %s. ' % choices[ComDraw]
    if UserDraw == ComDraw:
        print result + 'It is a tie.'
    elif UserDraw == (ComDraw + 1) % 3:
        print result + 'You win.'
        UserScore += 1
    else:
        print result + 'You lose.'
        ComScore += 1

print "Your Score: %d" % UserScore
print "The computer's Score: %d" % ComScore
5
BlogtiemOP•

I'm a little confused by the index. Is that a visual representation or is that actual code? How would I implement it?

1
JohnnyJordaan•

What do you mean by 'the index'? I'm using the 'list.index()' method to retrieve the index of the string in the list. Is that what you're referring to?

1
BlogtiemOP•

It looked like this:

i   0 1 2   
    S R P 
0 R W T L  
1 P L W T  
2 S T L W  
1
JohnnyJordaan•

Ah that's just a table to show the results of the possible combinations of Rock, Paper Scissors. The rows represent the user's draw and the columns the computer's. The intersection of the two represents the result for the user, in the form of Win, Loss or Tie. The implementation is in the code, the if statements determine the outcome in the order of T, W and L.

1
Poliorcetyks•

Instead of comparing with A and a you can compare with STR.lower() simplify your conditions :)

Three of your comparisons are useless, to check if it's a draw you probably should do this:

if com_choice.lower() == user_choice.lower():
    print "Draw !"
elif ....

(There are lots of other things but on mobile it's hard to explain, plus I never used 2.7 only 3.x so I might be missing things)

3
BlogtiemOP•

Fixed. It seems so obvious to me now, haha. If you get a chance, I'm still interested in the other feedback you have.

1
4evrAStudent•

Cool stuff, here are a couple of places to go from here:

  1. Rewrite using object-oriented code.

  2. Throw a GUI on it. I would recommend using Tkinter.

2
BlogtiemOP•

The GUI does sound like a good idea. On my to-do list, thanks.

2