首页 文章

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

提问于
浏览
1

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

在Big Bang Theory中,Sheldon和Raj创造了一款新游戏:“rock-paper-scissors-lizard-Spock” . 游戏规则是:剪刀剪纸;纸覆盖岩石;岩石碾碎蜥蜴;蜥蜴毒药斯波克; Spock砸碎剪刀;剪刀斩首蜥蜴;蜥蜴吃纸;论文反驳了斯波克; Spock蒸发岩石;岩石压碎剪刀 . 在Sheldon获胜的情况下,他会说:“Bazinga!”;如果Raj赢了,Sheldon会宣称:“Raj被骗了”;在关系中,他会要求一个新游戏:“再次!” . 考虑到两者选择的选项,制作一个程序,打印谢尔顿对结果的反应 . 输入包含一系列测试用例 . 第一行包含正整数T(T≤100),表示测试用例的数量 . 每个测试用例由输入行表示,分别包含Sheldon和Raj的选项,用空格分隔 .

我对这个问题的代码是

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

但我觉得它太久了 . 有没有办法减少它?

3 回答

  • 0

    首先,祝贺你试着写这篇文章!第一次尝试你的逻辑非常好 .

    下一步是制作一个数据结构,您可以按照相同的方式查询规则 . 一个很好的契合是 dictionary

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

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

    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))
    
  • 0
    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
    
  • 7

    试试这个,使用词典

    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))
    

相关问题