0

我是 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
4

5 回答 5

4

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

  • 阅读 ZEN(输入import this你的 python 控制台)。
  • 在您的特定情况下,很多条件通常不是一个好主意。

至少,所有 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

此外,您甚至不使用上面定义的 variablesp和....rs

于 2012-08-09T10:52:21.170 回答
3

一些建议:

  1. 您所有的条件案例都包含轮变量增加,除非发生错误的数据输入,因此您可以将round += 1行放在上面,并且在其他情况下只减少一次轮变量

  2. 你有 if 案例做同样的工作,例如当“领带!” 发生了;最好将此类情况分组。'领带!' 案例可以在一个条件下分组选择 == cpu_choice从而省略 3 个 elif 子句。想想其他游戏案例中的相同问题。

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

于 2012-08-09T10:50:20.407 回答
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
于 2012-08-09T10:55:50.277 回答
1

不要重复自己:

  • rounds += 1每一轮都会发生,所以你不必在每个分支中都放置
  • 打印结果数字也总是发生。
  • 使用四个空格缩进你的代码
于 2012-08-09T10:45:19.593 回答
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!!!
"""
于 2012-08-09T11:01:05.757 回答