4
\$\begingroup\$

I am a beginner in programming and this is the third program I've written. This is an interactive game of rock, paper, scissors, capable of being modified to any number of turns. I would love to see how a more experienced programmer could simplify this and make it smaller. Please let me know how I could change this program to be easier to understand for other programmers or any general layout/design rules I should keep in mind when writing future programs. I just have the slightest feeling that this code could be more compact.

args = ['rock', 'paper', 'scissors']
user_score = 0
comp_score = 0
turn = 3
print 'ROCK, PAPER, SCISSORS!'
print 'Best out of %d' % (turn)
while turn > 0:
    user = raw_input('Choose ' + ", ".join(args) + ' > ')
    comp = random.choice(args)
    proof = 'Computer: %s' % (comp)
    if user == 'q':
        break
    elif (user == args[0] and comp == args[2]) or (user == args[1]\
          and comp == args[0]) or (user == args[2] and comp == args[1]):
        print proof
        print 'Win \n'
        turn -= 1
        user_score += 1
    elif user == comp:
        print proof
        print 'Tie \n'
        turn -= 1
    elif user not in args:
        print 'INVALID INPUT \n'
    else:
        print proof
        print 'Lose \n'
        turn -= 1
        comp_score += 1 
print '\n User scored: %d \n Computer scored: %d \n' % (user_score,comp_score)
if user_score > comp_score:
    print 'Winner!'
elif user_score < comp_score:
    print 'Loser'
else:
    print 'Draw'
\$\endgroup\$

migrated from stackoverflow.com Feb 20 '15 at 13:49

This question came from our site for professional and enthusiast programmers.

6
\$\begingroup\$

This is pretty good, but there are a few things that needed to be improved. Here are a few of those things, specifically regarding your, odd, design.

  • Using % for string formatting is deprecated. To format strings, you should use str.format. For example, you'd do this: print 'Best out of {0}'.format(turn).
  • Your naming is, strange. For example, I'd rename args to possible_moves. Good variable names should reflect the purpose of the variable.
  • The way you've designed this is odd. I'd reccomend an object-oriented-design. with a Game class with certain methods and attributes to manage a game. A few things that I'd reccomend implementing would be:
    • A total_turns attribute which would allow there to be a custom amount of turns.
    • A Game.pick_move to choose a random choice.

That's about all I can think of for improvement. If there's anything that you want me to comment on, just tell me about it. I hope this helps!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy