In: Computer Science
Why is 1 example of my code failing?
def current_player_score(score_one: int, score_two:
int,current_player: str) -> int:
"""Return <score_one> if the <current_player>
represents player one, or
<score_two> if the <current_player> represents player
two.
>>> current_player_score(2, 4, "Player one")
2
>>> current_player_score(2, 3, "Player two")
3
"""
if current_player == PLAYER_ONE:
return score_one
return score_two
You code failed because in function current_player_score() , current_player is declared as string type but in if statement you were using PLAYER_ONE which is undefine inside this function.
Solution of this problem are
1) . Use if current_player == "Player one": in condition
2) Declared a variable inside function as
PLAYER_ONE=Player one then you code will work fine
Here is a code for both solution:
===============================
Solution 1
def current_player_score(score_one: int, score_two:
int,current_player: str) -> int:
"""Return <score_one> if the <current_player>
represents player one, or
<score_two> if the <current_player> represents player
two.
>>> current_player_score(2, 4, "Player one")
2
>>> current_player_score(2, 3, "Player two")
3
"""
if current_player == "Player one":
return score_one
return score_two
Soluiton 2
def current_player_score(score_one: int, score_two:
int,current_player: str) -> int:
"""Return <score_one> if the <current_player>
represents player one, or
<score_two> if the <current_player> represents player
two.
>>> current_player_score(2, 4, "Player one")
2
>>> current_player_score(2, 3, "Player two")
3
"""
PLAYER_ONE="Player one"
if current_player == PLAYER_ONE:
return score_one
return score_two
Code Snapshot:
=====================
Solution 1
=======================
Solution 2
=================
Output
===============