0

I'm getting PEP257 error with this part of code. I'm aware that adding more than 3 if/elifs causes this, but I'm unsure how to properly fix this.

I've tried to group up the ifs/elifs but that made it too complicated for me and I havent found any other good solutions from Dr. Google.

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."

Should not show the PEP257 error.

Martin
  • 23
  • 1
  • 6
  • What tool shows you PEP257 error? – sanyassh Sep 29 '19 at 16:52
  • That's not an error; thats a warning related to the docstring usage. It likely elaborated on what it thought the problem was. See [here](https://www.python.org/dev/peps/pep-0257/). It might be because you have the opening `"""` on its own line. – Carcigenicate Sep 29 '19 at 16:54

1 Answers1

0

Make use of the fact that return ends the execution.

    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."
andole
  • 286
  • 1
  • 7