Слишком сложная ошибка PEP257, не знаете, как ее исправить. - PullRequest
0 голосов
/ 29 сентября 2019

Я получаю ошибку PEP257 с этой частью кода. Я знаю, что добавление более 3, если / elifs вызывает это, но я не уверен, как правильно это исправить.

Я пытался сгруппировать ifs / elifs, но это сделало его слишком сложным дляЯ и я не нашли других хороших решений от доктора Гугла.

def determine_winner(name: str, user_choice: str, computer_choice: str, reverse_name: bool = False) -> str:
    """
    Determine the winner returns a string that has information about who won.

    You should use the functions that you wrote before. You should use check_user_choice function
    to validate the user choice and use normalize_user_name for representing a correct name. If the
    function parameter reverse is true, then you should also reverse the player name.
    NB! Use the previous functions that you have written!

    :param name:player name

    :param user_choice:
    :param computer_choice:
    :param reverse_name:
    :return:
    """
    user_choice = user_choice.lower()
    computer_choice = computer_choice.lower()
    if computer_choice == "rock":
        pass
    elif computer_choice == "paper":
        pass
    elif computer_choice == "scissors":
        pass
    else:
        return "There is a problem determining the winner."
    if reverse_name:
        name = reverse_user_name(name)
    if user_choice == computer_choice:
        return normalize_user_name(
            name) + " had " + user_choice + " and computer had " + computer_choice + ", hence it is a draw."
    elif user_choice == "rock":
        if computer_choice == "paper":
            return normalize_user_name(
                name) + " had " + user_choice + " and computer had " + computer_choice + ", hence computer wins."
        else:
            return normalize_user_name(
                name) + " had " + user_choice + " and computer had " + computer_choice + ", hence " + \
                   normalize_user_name(name) + " wins."
    elif user_choice == "scissors":
        if computer_choice == "paper":
            return normalize_user_name(
                name) + " had " + user_choice + " and computer had " + computer_choice + ", hence " + \
                   normalize_user_name(name) + " wins."
        else:
            return normalize_user_name(
                name) + " had " + user_choice + " and computer had " + computer_choice + ", hence computer wins."
    elif user_choice == "paper":
        if computer_choice == "scissors":
            return normalize_user_name(
                name) + " had " + user_choice + " and computer had " + computer_choice + ", hence computer wins."
        else:
            return normalize_user_name(
                name) + " had " + user_choice + " and computer had " + computer_choice + ", hence " + \
                   normalize_user_name(name) + " wins."
    else:
        return "There is a problem determining the winner."

Не должно отображаться ошибка PEP257.

1 Ответ

0 голосов
/ 29 сентября 2019

Используйте тот факт, что return завершает выполнение.

    user_choice = user_choice.lower()
    computer_choice = computer_choice.lower()

    if computer_choice not in {"rock", "paper", "scissors"}:
        return "There is a problem determining the winner."

    if reverse_name:
        name = reverse_user_name(name)

    choices = normalize_user_name(name) + " had " + user_choice + " and computer had " + computer_choice

    if user_choice == computer_choice:
        return choices + ", hence it is a draw."

    if user_choice == "rock":
        if computer_choice == "paper":
            return choices + ", hence computer wins."
        return choices + ", hence " + normalize_user_name(name) + " wins."

    if user_choice == "scissors":
        if computer_choice == "paper":
            return choices + ", hence " + normalize_user_name(name) + " wins."
        return choices + ", hence computer wins."

    if user_choice == "paper":
        if computer_choice == "scissors":
            return choices + ", hence computer wins."
        return choices + ", hence " + normalize_user_name(name) + " wins."

    return "There is a problem determining the winner."
...