摇滚,纸,剪刀 - 蟒蛇代码。帮助简化

时间:2012-08-09 10:37:53

标签: python

我是Python的新手,我只编写了几个程序。这是我为 rock-paper-scissors 游戏编写的最新代码。我已经测试过了,效果很好。有什么办法可以简化吗?谢谢!

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

5 个答案:

答案 0 :(得分:4)

虽然stackoverflow并不是真正的学习平台,但这里有一些建议:

  • 阅读ZEN(在您的python控制台中输入import this)。
  • 在您的特定情况下,充足的条件通常是一个坏主意。

至少,所有TIE条件都可以抛在一起:

 if choice == cpu_choice:
    # TIE

抛出一些语法:

names = ['rock', 'paper', 'scissors']
print("Computer chooses {}, you loose".format(names[cpu_choice]))

基本上,只有三个条件:

wins, losses = 0, 0

for round in range(10):

    # Your choice and CPU choice

    cpu_wins = (cpu_choice > choice or (choice == 3 and cpu_choice == 1))
    tie = (cpu_choice == choice)

    if cpu_wins:
        # You loose
        print("Computer chooses {}, you loose".format(names[cpu_choice]))
        losses += 1
    if not cpu_wins and tie:
        # tie
    else:
        # you win

此外,您甚至不使用上面定义的变量prs ....

答案 1 :(得分:3)

一些建议:

  1. 所有条件情况都包含舍入变量增加,除非发生错误的数据输入,因此您可以将 round + = 1 行引出上限,并在其他情况下仅将一次变量舍入变量

  2. 如果案件执行相同的工作,例如当'TIE!'发生;最好将此类案件分组。 '领带!'案例可以在一个条件 choice == cpu_choice 下分组,从而省略3个elif子句。想想其他游戏案例中的同样问题。

  3. 使用更好的代码格式,例如PEP-8标准建议的内容。

答案 2 :(得分:2)

您可以使用模运算确定玩家是否获胜:

player_result = ["tie", "win", "lose"]
player_choice = input('(1)rock, (2)paper, or (3)scissors? :')
cpu_choice= random.randint(1, 3)
result = player_result[(player_choice - cpu_choice) % 3]

print "You " + result
if result == "win":
    wins += 1
elif result == "lose":
    loses += 1

答案 3 :(得分:1)

不要重复自己:

  • rounds += 1发生在每一轮,所以你不必把每个分支都放进去
  • 打印结果编号也一直发生。
  • 使用四个空格缩进代码

答案 4 :(得分:0)

我会做这样的事情,这可能会比你的水平高一点,但如果你研究这段代码是如何工作的,那么你在pythoN会更好! :)

from random import randint

def do_rounds(num_rounds):
    choice_dict = {1: 'rock', 2: 'paper', 3: 'scissors'}
    beats_dict = {1: 3, 2: 1, 3: 2}

    for round in range(num_rounds):
        computer_choice = randint(1, 3)
        while True:
            player_choice = raw_input('(1)rock, (2)paper, or (3)scissors? :')
            if player_choice in ("1", "2", "3"):
                player_choice = int(player_choice)
                break
            else:
                print "input must be an integer 1, 2 or 3"

        player_lost = beats_dict[computer_choice] == player_choice

        tie = 1 if computer_choice == player_choice else 0
        win = 0 if player_lost else 1
        loss = 1 if player_lost else 0
        print "computer picked: %s" % choice_dict[computer_choice],
        print " you picked: %s" % choice_dict[player_choice]
        yield tie, win, loss

def run_game():
    ties, wins, losses = zip(*do_rounds(4))
    ties, wins, losses = sum(ties), sum(wins), sum(losses)
    print "ties = %s, wins = %s, losses = %s" % (ties, wins, losses)
    if wins > losses:
        print "you won!"
    elif wins == losses:
        print "tie!"
    else:
        print "loser!!!"

if __name__ == "__main__":
    run_game()

"""
(1)rock, (2)paper, or (3)scissors? :3
computer picked: rock, you picked: scissors
(1)rock, (2)paper, or (3)scissors? :2
computer picked: paper, you picked: paper
(1)rock, (2)paper, or (3)scissors? :1
computer picked: paper, you picked: rock
(1)rock, (2)paper, or (3)scissors? :3
computer picked: rock, you picked: scissors
ties = 1, wins = 1, losses = 3
loser!!!
"""