0

I am attempting to create a function with the following characteristics;

function name: new_scores,

parameters: (p1_score,p2_score,current_score,current_player)

p1_score: user inputed player 1 score,

p2_score: user inputed player 2 score,

current_score: the current score of set player after any actions performed

current_player: either 'PLAYER_ONE' or 'PLAYER_TWO'

the problem with my code is that it doesnt work in all cases, for example try replacing values in PLAYER_ONE, it just spits back out the p1_score, and p2_score that i inputed. Thanks for the help

def new_scores(p1_score,p2_score,current_score,current_player):
    if current_player == 'PLAYER_ONE':
        p1_score = current_score
        return (p1_score,p2_score)
    elif current_player == 'PLAYER_TWO':
        p2_score = current_score
        return (p1_score,p2_score)
1
  • It would be nice to see the code you use to call this function :) Commented Oct 19, 2012 at 12:44

2 Answers 2

2

Your code is needlessly complex, which is quite the feat for so few lines. :) There's no point in the assignment, just return the intended data directly:

def new_scores(p1_score, p2_score, current_score, current_player):
   if current_player == 'PLAYER_ONE':
      return (current_score, p2_score)
   elif current_player == 'PLAYER_TWO':
      return (p1_score, current_score)

Still, your code looks correct. Verify that the parameters are correct, using strings for semantics like this is a bit error-prone.

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure you are using the returned value and not expecting the parameters you passed in to be altered as a side effect.

>>> new_scores(3, 4, 5, "PLAYER_ONE")
(5, 4)
>>> new_scores(3, 4, 7, "PLAYER_TWO")
(3, 7)

Running your function as above shows it works as expected

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.