I’m new to Python, and I’ve only written a couple programs. Here’s a recent code I wrote for a rock-paper-scissors game. I’ve already tested it and it works great. Is there any way I can simplify it? Thanks!
import random
wins=0
losses=0
ties=0
rounds=0
r=1 #rock
p=2 #paper
s=3 #scissors
y = "The computer has made its choice, how about you?"
while rounds <= 10:
print y
x = input('(1)rock, (2)paper, or (3)scissors? :')
choice = x
cpu_choice= random.randint(1, 3)
if (choice, cpu_choice) == (1, 2):
rounds += 1
losses += 1
print 'computer chose paper, you lose'
elif (choice, cpu_choice) == (3, 2):
print 'you win'
rounds += 1
wins += 1
elif (choice, cpu_choice) == (2, 2):
print 'TIE!'
rounds += 1
ties += 1
elif (choice, cpu_choice) == (1, 3):
print 'you win'
rounds += 1
wins += 1
elif (choice, cpu_choice) == (3, 3):
print 'TIE!'
rounds += 1
ties += 1
elif (choice, cpu_choice) == (2, 3):
print 'computer chose scissors, you lose'
rounds += 1
losses += 1
elif (choice, cpu_choice) == (1, 1):
print 'TIE'
rounds += 1
ties += 1
elif (choice, cpu_choice) == (3, 1):
print 'computer chose rock, you lose'
rounds += 1
losses += 1
elif (choice, cpu_choice) == (2, 1):
print 'you win'
rounds += 1
wins += 1
else:
print 'Please choose 1, 2, or 3'
print 'Game Over'
if wins>losses:
print 'YOU WON'
print 'wins:' , wins
print 'losses' , losses
print 'ties' , ties
else:
print 'you lose'
print 'wins:' , wins
print 'losses:' , losses
print 'ties:' , ties
While stackoverflow is not really meant as a learning platform, here are some advises:
import thisinto your python console).At the very least, all TIE conditions can be thrown together:
Throw in some grammar:
Essentially, there are only three conditions:
Also, you don’t even use the variables
p,randsdefined above….