PEP257 错误太复杂,不确定如何修复
Too complex PEP257 error, unsure how to fix it
这部分代码出现 PEP257 错误。我知道添加超过 3 个 if/elifs 会导致此问题,但我不确定如何正确解决此问题。
我曾尝试对 ifs/elifs 进行分组,但这对我来说太复杂了,我还没有从 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."
不应显示 PEP257 错误。
利用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."
这部分代码出现 PEP257 错误。我知道添加超过 3 个 if/elifs 会导致此问题,但我不确定如何正确解决此问题。
我曾尝试对 ifs/elifs 进行分组,但这对我来说太复杂了,我还没有从 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."
不应显示 PEP257 错误。
利用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."