“石头-布-剪刀”逻辑条件的Python简化

2024-10-04 09:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

The rules of the game are:

  • scissors cuts paper;
  • paper covers rock;
  • rock crushes lizard;
  • lizard poisons Spock;
  • Spock smashes scissors;
  • scissors decapitates lizard;
  • lizard eats paper;
  • paper disproves Spock;
  • Spock vaporizes rock;
  • rock crushes scissors.

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.

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

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


Tags: orandofthetruewinpaperscissors
3条回答
T = int(input())

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

    if(Sheldon == Raj):
        Tie = True
    elif((Sheldon == "scissors" and (Raj in ["paper","lizard"])) or
         (Sheldon == "lizard" and (Raj in ["paper","Spock"])) or
         (Sheldon == "Spock" and (Raj in ["rock","scissors"])) or
         (Sheldon == "paper" and (Raj in ["rock","Spock"])) or
         (Sheldon == "rock" and (Raj in ["scissors","lizard"]))
        ):
        Win = True
    else:
        Lose = 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

试试这个,用字典

T = int(input())

for i in range(T):

    rules=  {
        "rock":     {"rock":0, "paper":-1,"scissors":1,"lizard":1,"Spock":-1},
        "paper":    {"rock":1, "paper":0,"scissors":-1,"lizard":-1,"Spock":1},
        "scissors": {"rock":-1, "paper":1,"scissors":0,"lizard":1,"Spock":-1},
        "lizard":   {"rock":1, "paper":-1,"scissors":1,"lizard":0,"Spock":-1},
        "Spock":    {"rock":1, "paper":-1,"scissors":1,"lizard":-1,"Spock":0}
        }
    Sheldon, Raj = input().split(' ') 

    Result = rules[Sheldon][Raj]
    if(Result == 1):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Result == -1):
        print("Case #{0}: Raj cheated!".format(i+1))
    else:
        print("Case #{0}: Again!".format(i+1))

首先,祝贺你写这篇文章!你的逻辑很适合第一次尝试。在

下一步是为规则创建一个可以用相同方式查询的数据结构。一个合适的选择是dictionary

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

然后您就可以查询它,而不是重复大量的if

^{pr2}$

相关问题 更多 >