patternpythonMinor
Interactive Rock Paper Scissors
Viewed 0 times
scissorspaperrockinteractive
Problem
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'Solution
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.
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!
- Using
%for string formatting is deprecated. To format strings, you should usestr.format. For example, you'd do this:print 'Best out of {0}'.format(turn).
- Your naming is, strange. For example, I'd rename
argstopossible_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
Gameclass with certain methods and attributes to manage a game. A few things that I'd reccomend implementing would be:
- A
total_turnsattribute which would allow there to be a custom amount of turns.
- A
Game.pick_moveto 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!
Context
StackExchange Code Review Q#82068, answer score: 6
Revisions (0)
No revisions yet.