Python——简化“石头剪刀布"逻辑条件 [英] Python - Simplification of "rock paper scissors" logic conditions

查看:61
本文介绍了Python——简化“石头剪刀布"逻辑条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个问题,它说:

I was solving a question and it said:

在 Big Bang Theory 中,Sheldon 和 Raj 创造了一个新游戏:石头剪刀布蜥蜴史波克".

In Big Bang Theory, Sheldon and Raj created a new game: "rock-paper-scissors-lizard-Spock".

游戏规则是:

  • 剪刀剪纸;
  • 纸覆盖岩石;
  • 岩石压碎蜥蜴;
  • 蜥蜴毒害史波克;
  • 史波克打碎剪刀;
  • 剪刀斩首蜥蜴;
  • 蜥蜴吃纸;
  • 论文反驳了斯波克;
  • Spock 使岩石蒸发;
  • 石头压碎剪刀.

在谢尔顿获胜的情况下,他会说:Bazinga!";如果 Raj 赢了,Sheldon 会宣布:Raj 被骗了";在平局中,他会要求一个新游戏:再一次!".给定双方选择的选项,制作一个程序,打印谢尔顿对结果的反应.

In the case of Sheldon's victory, he would've said: "Bazinga!"; if Raj had won, Sheldon would declare: "Raj cheated"; in ties, he would request a new game: "Again!". Given the options chosen by both, make a program that prints Sheldon reaction to the outcome.

输入由一系列测试用例组成.第一行包含一个正整数 T(T ≤ 100),表示测试用例的数量.每个测试用例由一行输入表示,分别包含 Sheldon 和 Raj 的选择,用空格分隔.

The input consists of a series of test cases. The first line contains a positive integer T (T ≤ 100), which represents the number of test cases. Each test case is represented by a line of the input, containing the choices of Sheldon and Raj, respectively, separated by a space.

我对这个问题的代码是

T = int(input())

for i in range(T):
    Sheldon, Raj = input().split(' ')

    if(Sheldon == "scissors" and (Raj == "paper" or Raj == "lizard")):
        Win = True
    elif(Sheldon == "lizard" and (Raj == "paper" or Raj == "Spock")):
        Win = True
    elif(Sheldon == "Spock" and (Raj == "rock" or Raj == "scissors")):
        Win = True
    elif(Sheldon == "paper" and (Raj == "rock" or Raj == "Spock")):
        Win = True
    elif(Sheldon == "rock" and (Raj == "scissors" or Raj == "lizard")):
        Win = True
    elif(Raj == "scissors" and (Sheldon == "paper" or Sheldon == "lizard")):
        Lose = True
    elif(Raj == "lizard" and (Sheldon == "paper" or Sheldon == "Spock")):
        Lose = True
    elif(Raj == "Spock" and (Sheldon == "rock" or Sheldon == "scissors")):
        Lose = True
    elif(Raj == "paper" and (Sheldon == "rock" or Sheldon == "Spock")):
        Lose = True
    elif(Raj == "rock" and (Sheldon == "scissors" or Sheldon == "lizard")):
        Lose = True
    elif(Sheldon == Raj):
        Tie = True

    if(Win == True):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Lose == True):
        print("Case #{0}: Raj cheated!".format(i+1))
    elif(Tie == True):
        print("Case #{0}: Again!".format(i+1))

    Win = Lose = Tie = False

但我觉得时间太长了.有什么办法可以减少吗?

but I thought it got too long. Is there any way to reduce it?

推荐答案

首先,恭喜你尝试写这篇文章!您的逻辑非常适合第一次尝试.

First of all, congratulations on attempting to write this! Your logic is pretty good for a first attempt.

下一步是创建一个数据结构,您可以以相同的方式查询规则.dictionary 很合适:

The next step is making a data structure you can query in the same way, for the rules. A good fit would be a dictionary:

options = {
 'scissors': ('paper', 'lizard'),
 'paper': ('rock', 'spock'),
 'rock': ('lizard', 'scissors'),
 'lizard': ('spock', 'paper'),
 'spock': ('scissors', 'rock'),
}

然后你可以查询它而不是重复很多if:

Then you can just query it instead of repeating lots of ifs:

if raj == sheldon:
   print("Case #{0}: Again!".format(i+1))
elif raj in options[sheldon]:
   print("Case #{0}: Bazinga!".format(i+1))
else: 
   print("Case #{0}: Raj cheated!".format(i+1))

这篇关于Python——简化“石头剪刀布"逻辑条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆