首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >石头,纸,剪刀-蟒蛇-密码。帮助简化

石头,纸,剪刀-蟒蛇-密码。帮助简化
EN

Stack Overflow用户
提问于 2012-08-09 10:37:52
回答 5查看 6.9K关注 0票数 0

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

代码语言:javascript
复制
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
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-08-09 10:52:21

虽然堆栈溢出并不是真正意义上的学习平台,但以下是一些建议:

  • 阅读ZEN (在您的python控制台中键入import this )。
  • 在你的特殊情况下,很多条件通常都是个坏主意。

至少,所有的绑定条件都可以组合在一起:

代码语言:javascript
复制
 if choice == cpu_choice:
    # TIE

加上一些语法:

代码语言:javascript
复制
names = ['rock', 'paper', 'scissors']
print("Computer chooses {}, you loose".format(names[cpu_choice]))

基本上,只有三个条件:

代码语言:javascript
复制
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 .

票数 4
EN

Stack Overflow用户

发布于 2012-08-09 10:50:20

一些建议:

  1. 除了出现错误的数据输入时,所有条件情况下都包含圆型变量增加,因此可以将循环的+= 1线取上,而在其他情况下则只减少一次循环变量。
  2. 如果案件做同样的工作,例如“打成平手!”已经发生了,最好把这类案件分组。“‘TIE!”案例可以按一个条件分组,选择== cpu_choice,从而包含3个elif子句。在其他游戏案例中考虑同样的问题。
  3. 使用更好的代码格式,例如佩普-8标准所建议的。
票数 3
EN

Stack Overflow用户

发布于 2012-08-09 10:55:50

您可以使用模块化算法来确定玩家是否获胜:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11881539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档