我是 Python 新手,我只写了几个程序。这是我最近为剪刀石头布游戏编写的代码。我已经对其进行了测试,并且效果很好。有什么办法可以简化它吗?谢谢!
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