如何减少我的代码冗余?

时间:2014-02-12 11:16:24

标签: python

我想让这个程序减少更少,但我无法弄清楚如何。一方面,这个过程有一个模式,我觉得有一个更好的方法,而不是手动说明每一个可能的结果。另一方面,每种情况在其自身方面都是独一无二的。关于如何简化此代码的任何想法?

def calculate_Winner(user, npc):


    if user == 0 and npc == 0:
        return "The computer is scissor. You are scissor too. It is a draw."
    elif user == 0 and npc == 1:
        return "The computer is rock. You are scissor. You lose"
    elif user == 0 and npc == 2:
        return "The computer is paper. You are scissor. You win (;"


    elif user == 1 and npc == 0:
        return "The computer is scissor. You are rock. You win"
    elif user == 1 and npc == 1:
        return "The computer is rock. You are rock. It is a draw."
    elif user == 1 and npc == 2:
        return "The computer is paper. You are rock. You lose."


    elif user == 2 and npc == 0:
        return "The computer is scissor. You are paper. You lose"
    elif user == 2 and npc == 1:
        return "The computer is rock. You are paper. You win"
    elif user == 2 and npc == 2:
        return "The computer is paper. You are paper too. Draw"

4 个答案:

答案 0 :(得分:5)

move_names = ['scissor', 'rock', 'paper']
winner_strings = ["It's a draw.", "You win.", "You lose."]

def get_result_string(player_move, computer_move):
    winner = player_move - computer_move % 3
    return 'You played {}. The computer played {}. {}'.format(
            move_names[player_move],
            move_names[computer_move],
            winner_strings[winner])

答案 1 :(得分:1)

这个怎么样?

MAP = {
    0: "scissor",
    1: "rock",
    2: "paper"
}
KILLS = {
    0: 2,
    1: 0,
    2: 1
}

def winner(user, npc):
    if user == npc:
        return "Draw."
    elif KILLS[user] == npc:
        return "You win."
    elif KILLS[npc] == user:
        return "You lose."

def calculate_Winner(user, npc):
    res = "The computer is %s. " % MAP[npc]
    res += "You are %s. " % MAP[user]
    res += winner(user, npc)
    return res

答案 2 :(得分:0)

如果您想保留每个响应的唯一性,同时避免混乱的if-else语句,请使用Python tuple作为字典键:

#!/usr/bin/env python

import sys

def calculate_Winner(user, npc):
    result = {
        (0, 0) : "The computer is scissor. You are scissor too. It is a draw.",
        (0, 1) : "The computer is rock. You are scissor. You lose",
        (0, 2) : "The computer is paper. You are scissor. You win",
        (1, 0) : "The computer is scissor. You are rock. You win",
        (1, 1) : "The computer is rock. You are rock. It is a draw.",
        (1, 2) : "The computer is paper. You are rock. You lose.",
        (2, 0) : "The computer is scissor. You are paper. You lose",
        (2, 1) : "The computer is rock. You are paper. You win",
        (2, 2) : "The computer is paper. You are paper too. Draw"
        }
    return result[(user, npc)]

sys.stdout.write(calculate_Winner(0, 2) + "\n");
sys.stdout.write(calculate_Winner(2, 1) + "\n");

答案 3 :(得分:0)

def calculate_Winner(user, npc):
    rpcStr = ["rock", "paper", "scissor"]
    outcomeStr = ["It is a draw", "You lose", "You win (;"]
    winMap = [[0,1,2],[2,0,1],[1,2,0]]
    too = " too" if user == npc else ""
    return "The computer is {0}. You are {1}{2}. {3}".format(rpcStr[user], rpcStr[npc], too, outcomeStr[winMap[user][npc]])

这产生与最少代码中的程序完全相同的输出。 if语句仅在应该使用的地方使用,并且,更改字符串的内容非常容易。

rpcStr拥有可能的岩纸剪刀弦。

outcomeStr包含可能的结果字符串。

winMap根据if语句将用户和npc值映射到所需的输出字符串索引。例如:

elif user == 1 and npc == 1:
        return "The computer is rock. You are rock. It is a draw." 

user = 1,npc = 1.当你在index [1] [1]获得winmap的元素时,你会得到数字0,它是引用绘图的outcomeStr的索引。

如果用户和npc相等(例如抽奖)

也会“太”

最后一行只是将所有这些值格式化为字符串。如果你想让它更具可读性,你可以在更多的行中执行此操作,例如:

rpcUser = rpcStr[user]
rpcNpc = rpcStr[npc]
outcome = outcomeStr[winMap[user][npc]]
return "The computer is {0}. You are {1}{2}. {3}".format(rpcUser,rpcNpc,too,outcome)