Home > front end >  I need to Create a function switchPlayer that requires 1 parameter named curr_player. If curr_player
I need to Create a function switchPlayer that requires 1 parameter named curr_player. If curr_player

Time:10-25

We assume curr_player to be 'X'. Output should be 'O', and vice-versa

(is my code correct?)

 #my code 
    def switchPlayer(curr_player):
        if curr_player == "X" or curr_player == "x":
            print("your Player 'O' ")
            return  curr_player
        if curr_player == "O" or curr_player == "o":
            print("your Player 'X' ")
            return curr_player
    
    
    
    switchPlayer("X")

CodePudding user response:

You want to return the opposite of what it currently is. Something else I changed was the way you compare the strings. Instead of checking for uppercase and lowercase, you can convert the string to lowercase first, and you only need to compare it one time.

if (curr_player.lower() == 'x'):
  return 'O'
if (curr_player.lower() == 'o'):
  return 'X'

This code can also be simplified to this:

return 'O' if curr_player.lower() == 'x' else 'X'

CodePudding user response:

    def switchPlayer(curr_player):
        if curr_player == "X" or curr_player == "x":
            curr_player="O"
            return  curr_player
        if curr_player == "O" or curr_player == "o":
            curr_player="X"
            return curr_player    
    inp=input("input the current player")    
    a=switchPlayer(inp)
    print("returned player",a)
  • Related